fork download
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <algorithm>
  5. #include <string.h>
  6. #include <math.h>
  7. #include <limits.h>
  8. #include <sstream>
  9. #include <vector>
  10. using namespace std;
  11. #define rep(i,a,N) for(int i=a;i<N;++i)
  12. vector<int> result;
  13. int Outer(char x){
  14. return x-65;
  15. }
  16. int Inner(char x){
  17. return x-65+5;
  18. }
  19.  
  20. struct Out_Map{
  21. int x,y,z;
  22. }out[5];
  23.  
  24. struct Inn_Map{//map for inner graph
  25. int x,y,z;
  26. }inn[10];
  27.  
  28. void Initialize(){//to initialize the graph
  29. out[0].x=1;
  30. out[0].y=4;
  31. out[0].z=5;
  32.  
  33. out[1].x=0;
  34. out[1].y=2;
  35. out[1].z=6;
  36.  
  37. out[2].x=1;
  38. out[2].y=3;
  39. out[2].z=7;
  40.  
  41. out[3].x=2;
  42. out[3].y=4;
  43. out[3].z=8;
  44.  
  45. out[4].x=0;
  46. out[4].y=8;
  47. out[4].z=9;
  48.  
  49. inn[9].x=4;
  50. inn[9].y=6;
  51. inn[9].z=7;
  52.  
  53. inn[5].x=0;
  54. inn[5].y=7;
  55. inn[5].z=8;
  56.  
  57. inn[6].x=1;
  58. inn[6].y=8;
  59. inn[6].z=9;
  60.  
  61. inn[7].x=2;
  62. inn[7].y=5;
  63. inn[7].z=9;
  64.  
  65. inn[8].x=3;
  66. inn[8].y=5;
  67. inn[8].z=6;
  68.  
  69. }
  70.  
  71. bool isPresent(int tmp,int code){
  72. if(tmp<5){
  73. if(out[tmp].x==code || out[tmp].y==code || out[tmp].z==code)
  74. return true;
  75. return false;
  76. }
  77. else{
  78. if(inn[tmp].x==code || inn[tmp].y==code || inn[tmp].z==code)
  79. return true;
  80. return false;
  81. }
  82. }
  83.  
  84. bool Calc(int d,string str){//d to check whether outer or inner
  85. bool flag=true;
  86. int code;
  87. if(d==0)
  88. code=Outer(str.at(0));
  89. else
  90. code=Inner(str.at(0));
  91. result.push_back(code);
  92. rep(i,1,str.size()){
  93. //printf("%d\n",code );
  94. if(isPresent(code,Outer(str.at(i))))
  95. code=Outer(str.at(i));
  96. else if(isPresent(code,Inner(str.at(i))))
  97. code=Inner(str.at(i));
  98. else{
  99. flag=false;
  100. break;
  101. }
  102. result.push_back(code);
  103. }
  104.  
  105. return flag;
  106. }
  107.  
  108. void Print(){
  109. rep(i,0,result.size())
  110. cout<<result[i];
  111. printf("\n");
  112. }
  113. int main(){
  114. Initialize();
  115. int T;
  116. string str;
  117. scanf("%d",&T);
  118. while(T--){
  119. cin>>str;
  120. if(str.size()==1)
  121. printf("%d\n",Outer(str.at(0)));
  122. else{
  123. if(Calc(0,str)){
  124. Print();
  125. result.clear();
  126. }
  127. else{
  128. //printf("now clearing\n");
  129. result.clear();
  130. if(Calc(1,str)){
  131. Print();
  132. result.clear();
  133. }
  134. else{
  135. printf("-1\n");
  136. result.clear();
  137. }
  138. }
  139.  
  140. }
  141.  
  142. }
  143. return 0;
  144. }
  145.  
Success #stdin #stdout 0s 3436KB
stdin
1
ED
stdout
48