AOJ 2281 - Swap Cipher

http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2281

解法

逆からやるだけ.

 1 #include <iostream>
 2 #include <vector>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 int main()
 7 {
 8   int N;
 9   while (cin >> N && N != 0) {
10     string s;
11     cin >> s;
12     vector<pair<int,int> > v(N);
13     for (int i = 0; i < N; i++) {
14       cin >> v[i].first >> v[i].second;
15       --v[i].first; --v[i].second;
16     }
17 
18     for (vector<pair<int,int> >::const_reverse_iterator it = v.rbegin(); it != v.rend(); ++it) {
19       const int gap = abs(it->first - it->second);
20       swap(s[it->first], s[it->second]);
21       s[it->first] = ((s[it->first]-'a'+gap)%26)+'a';
22       s[it->second] = ((s[it->second]-'a'+gap)%26)+'a';
23     }
24     cout << s << endl;
25   }
26   return 0;
27 }
aoj/2281.cc