记录编号 67027 评测结果 AAAAAAAA
题目名称 [NOIP 2002]字串变换 最终得分 100
用户昵称 Gravatar老师好~~~ 是否通过 通过
代码语言 C++ 运行时间 4.435 s
提交时间 2013-08-07 10:37:26 内存使用 1.08 MiB
显示代码纯文本
  1. #include<fstream>
  2. #include<deque>
  3. #include<string>
  4. #include<cstdlib>
  5. #include<set>
  6. using namespace std;
  7. ifstream fin("string.in");
  8. ofstream fout("string.out");
  9. deque<string> b;
  10. string s1,s2;
  11. int G=0;
  12. class woca
  13. {
  14. public:
  15. string x;
  16. string y;
  17. }a[100000];
  18. class woqu
  19. {
  20. public:
  21. int x;
  22. string y;
  23. };
  24. set<woqu> ctr;
  25. set<woqu>::iterator cp;
  26. int K=1,co=1;
  27. /*string flag[100000];*/
  28. /*int e[100000];*/
  29. bool operator<(woqu m,woqu n)
  30. {
  31. if(m.y<n.y)
  32. return 1;
  33. else
  34. return 0;
  35. }
  36. bool operator==(woqu m,woqu n)
  37. {
  38. if(m.y==n.y)
  39. return 1;
  40. else
  41. return 0;
  42. }
  43. void BFS()
  44. {
  45. int l,s,i,k,m,jicun;
  46. string j;
  47. string temp;
  48. while(b.size()!=0)
  49. {
  50. j=b.at(0);
  51. b.pop_front();
  52. l=j.length();
  53. for(i=1;i<=K;i++)
  54. {
  55. s=a[i].x.length();
  56. for(k=0;k<=l-s;k++)
  57. {
  58. if(a[i].x==j.substr(k,s))
  59. {
  60. temp=j.substr(0,k)+a[i].y+j.substr(k+s,l-k-s);
  61. woqu ttt,ttt1;
  62. ttt.y=temp;
  63. if(ctr.find(ttt)==ctr.end())
  64. {
  65. b.push_back(temp);
  66. ttt1.y=j;
  67. cp=ctr.find(ttt1);//向平衡二叉树中查找父亲节点
  68. /*for(m=1;m<=co;m++)
  69. {
  70. if(flag[m]==j)
  71. jicun=e[m];
  72. }*/
  73. /*co++;
  74. flag[co]=temp;
  75. e[co]=jicun+1;*/
  76. co=(*cp).x+1;
  77. ttt.x=co;
  78. ctr.insert(ttt);//向平衡二叉树中插入元素
  79. if(co>10)
  80. {
  81. fout<<"NO ANSWER!";
  82. G=1;
  83. return;
  84. }
  85. if(temp==s2)
  86. {
  87. fout<<co;
  88. G=1;
  89. return ;
  90. }
  91. }
  92. }
  93. }
  94. }
  95. }
  96. }
  97. int main()
  98. {
  99. int i;
  100. fin>>s1>>s2;
  101. while(!fin.eof())
  102. {
  103. fin>>a[K].x>>a[K].y;
  104. if(a[K].x.length()!=0&&a[K].y.length()!=0)
  105. K++;
  106. }
  107. K--;
  108. woqu ll;
  109. ll.y=s1;
  110. ll.x=0;
  111. b.push_back(s1);
  112. ctr.insert(ll);
  113. BFS();
  114. if(G==0)
  115. fout<<"NO ANSWER!"<<endl;
  116. return 0;
  117. }