zoj 1009
View Code
1 #include <cstdio> 2 #include <iostream> 3 #include <algorithm> 4 using namespace std; 5 #include <string> 6 #include <deque> 7 8 const int N = 30; 9 int n,m; 10 string rotor[4]; 11 string tmp; 12 deque<int> level[4]; 13 deque<int> a,b,c; 14 15 void cover(); 16 void solve(); 17 int main() 18 { 19 int cas = 0; 20 while(cin >> m && m){ 21 if(cas++)cout << endl; 22 cin >> rotor[0] >> rotor[1] >> rotor[2]; 23 cover(); 24 cin >> n; 25 cout << "Enigma " << cas << ':' << endl; 26 while(n--){ 27 cin >> tmp; 28 solve(); 29 } 30 } 31 return 0; 32 } 33 void cover() 34 { 35 int x,y,offset,index; 36 level[0].resize(m); level[1].resize(m); level[2].resize(m); 37 for(x = 0; x < 3; ++x){ 38 for(y = 0; y < m; ++y){ 39 index = rotor[x][y] - 'A'; 40 offset = y - index ; 41 level[x][index] = offset; 42 } 43 } 44 } 45 void solve() 46 { 47 a = level[0]; b = level[1]; c = level[2]; 48 string txt; 49 char ch; 50 int k; 51 int s; 52 for(int i = 0; i < tmp.size(); ++i){ 53 ch = tmp[i]; 54 k = ch - 'A'; 55 56 s = (k + c[k] + m)%m; 57 k = (s + b[s] + m)%m; 58 s = (k + a[k] + m)%m; 59 60 txt += ('a' + s); 61 62 k = a.back(); a.pop_back(); a.push_front(k);//role a 63 64 if( (i+1) % m == 0){//role b 65 k = b.back(); b.pop_back(); b.push_front(k); 66 } 67 if( (i+1) % (m*m) == 0){//role c 68 k = c.back(); c.pop_back(); c.push_front(k); 69 } 70 } 71 cout << txt << endl; 72 }