Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.45 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <queue>
  4. #include <algorithm>
  5. #include <iostream>
  6. #include <vector>
  7. using namespace std;
  8.  
  9. int kong(int x) {
  10.     if (x < 0) x += 5;
  11.     return x % 5;
  12. }
  13.  
  14.  
  15. int n,sol;
  16. char s[100100],s1[100100];
  17.  
  18.  
  19. void print() {
  20.     for (int i = 0;i < n; i++)
  21.         printf("%c",s1[i]);
  22.     printf("\n");
  23. }
  24.  
  25. void kreni(int cvor,int k) {
  26.     k++;
  27.  
  28.     if (k == n) {
  29.         print();
  30.         sol = 0;
  31.         return;
  32.     }
  33.    
  34.     int next;
  35.        
  36.     if (cvor < 5) {
  37.         next = kong(cvor + 1);
  38.         if (next + 'A' == s[k]) {
  39.             s1[k] = next + 48;
  40.             kreni(next, k);
  41.            
  42.         }
  43.        
  44.         next = kong(cvor - 1);
  45.        
  46.         if (next + 'A' == s[k]) {
  47.             s1[k] = next + 48;
  48.             kreni(next, k);
  49.         }
  50.        
  51.         if (cvor + 'A' == s[k]) {
  52.             s1[k] = cvor + 5 + 48;
  53.             kreni(cvor + 5,  k );
  54.         }
  55.     }
  56.     else {
  57.         next = kong(cvor + 3) + 5;
  58.         if ((next - 5) + 'A' == s[k]) {
  59.             s1[k] = next + 48;
  60.             kreni(next, k );
  61.         }
  62.        
  63.         next = kong(cvor + 2) + 5;
  64.         if ((next - 5) + 'A' == s[k]) {
  65.             s1[k] = next + 48;
  66.             kreni(next, k );
  67.         }
  68.        
  69.         if ((cvor - 5) + 'A' == s[k]) {
  70.             s1[k] = cvor - 5 + 48;
  71.             kreni(cvor - 5, k);
  72.         }
  73.     }
  74. }
  75.  
  76. void solve() {
  77.     scanf("%s",s);
  78.    
  79.     n = strlen(s);
  80.    
  81.     sol = -1;
  82.    
  83.    
  84.     s1[0] = (s[0] - 'A') + 48;
  85.    
  86.     kreni(s[0] - 'A', 0);
  87.    
  88.     if (sol == 0) return;
  89.    
  90.     s1[0] = s[0] - 'A' + 5 + 48;
  91.    
  92.     kreni(s[0] - 'A' + 5, 0);
  93.    
  94.     if (sol == -1) printf("%d\n",sol);
  95.  
  96. }  
  97.  
  98.  
  99. int main() {
  100.     int t;
  101.     scanf("%d",&t);
  102.    
  103.     while (t--) solve();
  104.    
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement