记录编号 104290 评测结果 AAAAAAA
题目名称 [HAOI 2005]破译密文 最终得分 100
用户昵称 Gravatarztx 是否通过 通过
代码语言 C++ 运行时间 0.002 s
提交时间 2014-06-04 18:20:27 内存使用 0.34 MiB
显示代码纯文本
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4. #include <cstdlib>
  5. #include <queue>
  6.  
  7. using namespace std;
  8.  
  9. string s1 , s2;
  10. int sig[2]={2601,2602};
  11. int data1 [ 2601 ] = { 0 } , data2 [ 2601 ] = { 0 };
  12. int father [ 2601 + 1 ] = { 0 };
  13. int word [ 26 ] = { 0 };
  14. int ans = 1;
  15. bool f [ 2602 ] = { 0 };
  16.  
  17. void In();
  18. void Out();
  19. void Union(int x,int y);
  20. int Find(int x);
  21.  
  22. int main()
  23. {
  24. freopen("encrypt.in","r",stdin);
  25. freopen("encrypt.out","w",stdout);
  26. In();
  27. Out();
  28. return 0;
  29. }
  30.  
  31. void In()
  32. {
  33. char ch;
  34. int t = 0;
  35. cin >> s1 >> s2;
  36. cin >> t;
  37. while ( t -- )
  38. {
  39. cin >> ch;
  40. cin >> word [ ch - 'a' ];
  41. }
  42. for (int i = 0;i < s1.size();i ++ )
  43. {
  44. ch = s1[ i ];
  45. if (ch == '0' || ch == '1')
  46. data1 [ ++ data1 [ 0 ] ] = sig[ch-'0'];
  47. else
  48. {
  49. t = ch - 'a';
  50. for (int j = 1;j <= word [ t ];j ++ )
  51. data1 [ ++ data1 [ 0 ] ] = t * 100 + j;
  52. }
  53. }
  54. for (int i = 0;i < s2.size();i ++ )
  55. {
  56. ch = s2[ i ];
  57. if (ch == '0' || ch == '1')
  58. data2 [ ++ data2 [ 0 ] ] = sig[ch-'0'];
  59. else
  60. {
  61. t = ch - 'a';
  62. for (int j = 1;j <= word [ t ];j ++ )
  63. data2 [ ++ data2 [ 0 ] ] = t * 100 + j;
  64. }
  65. }
  66. }
  67.  
  68. void Out()
  69. {
  70. father [sig[0]]=sig[0];
  71. father [sig[1]]=sig[1];
  72. if ( data1 [ 0 ] != data2 [ 0 ])
  73. {
  74. cout << "0" << endl;
  75. return;
  76. }
  77. for ( int i = 1;i <= data1 [ 0 ];i ++ )
  78. {
  79. Union(data1 [ i ],data2 [ i ]);
  80. }
  81. for ( int i = 1;i <= data1 [ 0 ];i ++ )
  82. {
  83. int r = Find(data1 [ i ]);
  84. if ( r != sig[1] && r != sig[0] && ! f [ r ])
  85. {
  86. ans *= 2;
  87. f [ r ] = true;
  88. }
  89. }
  90. cout << ans << endl;
  91. }
  92.  
  93. void Union(int x,int y)
  94. {
  95. int r1 = Find(x);
  96. int r2 = Find(y);
  97. if (r1 == sig[0] && r2 == sig[1])
  98. {
  99. cout<<0<<endl;
  100. exit(0);
  101. }
  102. if (r1 == sig[1] && r2 == sig[0])
  103. {
  104. cout<<0<<endl;
  105. exit(0);
  106. }
  107. if ( r1 == r2) return ;
  108. if ( r1 > r2 ) father [ r2 ] = r1;
  109. else if ( r1 < r2 ) father [ r1 ] = r2;
  110. }
  111.  
  112. int Find(int x)
  113. {
  114. if ( father [ x ] == sig[0] || father[x] == sig[1]) return father[x];
  115. if ( father [ x ] == 0 || father [ x ] == x )
  116. {
  117. father [ x ] = x;
  118. return x;
  119. }
  120. father [ x ] = Find( father [ x ] );
  121. return father [ x ];
  122. }