[关闭]
@ysner 2018-06-06T11:24:12.000000Z 字数 1983 阅读 1829

正确答案

Tire map


题面

外卡组试卷中共有道判断题,小Y一共从其他个神犇那问了答案。
个神犇中有个考了满分,个考了零分,其他神犇不为满分或零分。
这可让小Y犯了难。你能帮助他们还原出标准答案吗?如有多解则输出字典序最小的那个。无解输出

解析

很显然,我们可以把这些神犇的答案变成一堆字符串。
然后若要标准答案,对这些字符串进行排序,找出个数恰好为的和反过来个数恰好为的字符串即可。

如何排序?
(注意空间复杂度为,要卡空间)都可以。
复杂度估计不超过

如果,直接暴搜即可,复杂度不超过
然而我不判有没有当前字符串才能AC是什么情况

  1. #include<iostream>
  2. #include<cmath>
  3. #include<cstring>
  4. #include<cstdio>
  5. #include<cstdlib>
  6. #include<algorithm>
  7. #define ll long long
  8. #define re register
  9. #define il inline
  10. #define fp(i,a,b) for(re int i=a;i<=b;i++)
  11. #define fq(i,a,b) for(re int i=a;i>=b;i--)
  12. using namespace std;
  13. int n,m,p,q,a[30005][505],tot,id[15000005][2],num[15000005],now[600],f;
  14. il ll gi()
  15. {
  16. re ll x=0,t=1;
  17. re char ch=getchar();
  18. while((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
  19. if(ch=='-') t=-1,ch=getchar();
  20. while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
  21. return x*t;
  22. }
  23. il void wri(re int x)
  24. {
  25. if(x<0) putchar('-'),x=-x;
  26. if(x>9) wri(x/10);
  27. putchar(x%10+'0');
  28. }
  29. il void dfs(re int x,re int u,re int dep)
  30. {
  31. if(dep==m) return;
  32. if(!a[x][dep+1])
  33. {
  34. if(!id[u][0]) id[u][0]=++tot,++num[tot];
  35. else ++num[id[u][0]];
  36. dfs(x,id[u][0],dep+1);
  37. }
  38. else
  39. {
  40. if(!id[u][1]) id[u][1]=++tot,++num[tot];
  41. else ++num[id[u][1]];
  42. dfs(x,id[u][1],dep+1);
  43. }
  44. }
  45. il int work(re int x,re int dep)
  46. {
  47. if(dep==m) return num[x];
  48. re int way=now[dep]^1;
  49. if(id[x][way]) return work(id[x][way],dep+1);
  50. else return 0;
  51. }
  52. il void print()
  53. {
  54. fp(i,0,m-1) putchar((now[i]^f)?'Y':'N');
  55. puts("");
  56. exit(0);
  57. }
  58. il void dfs1(re int x,re int dep)
  59. {
  60. if(dep==m)
  61. {
  62. if(num[x]!=p) return;
  63. if(work(0,0)==q) print();
  64. return;
  65. }
  66. if(id[x][0]) now[dep]=0,dfs1(id[x][0],dep+1);
  67. if(id[x][1]) now[dep]=1,dfs1(id[x][1],dep+1);
  68. }
  69. il void dfs2(re int x,re int dep)
  70. {
  71. if(dep==m)
  72. {
  73. if(num[x]!=p) return;
  74. if(work(0,0)==q) print();
  75. return;
  76. }
  77. now[dep]=0,dfs2(id[x][0],dep+1);
  78. now[dep]=1,dfs2(id[x][1],dep+1);
  79. }
  80. int main()
  81. {
  82. freopen("answer.in","r",stdin);
  83. freopen("answer.out","w",stdout);
  84. n=gi();m=gi();p=gi();q=gi();
  85. fp(i,1,n)
  86. fp(j,1,m)
  87. {
  88. re char ch=getchar();
  89. while(ch!='N'&&ch!='Y'){ch=getchar();}
  90. a[i][j]=(ch!='N');
  91. }
  92. fp(i,1,n) dfs(i,0,0);
  93. if(!p&&q) swap(p,q),f=1;
  94. if(p||q) dfs1(0,0);
  95. else dfs2(0,0);
  96. puts("-1");
  97. fclose(stdin);
  98. fclose(stdout);
  99. return 0;
  100. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注