題目連接:https://vjudge.net/problem/CodeForces-1156Bios
題意:給定一串字符,相鄰字符的ASCII碼不能是相鄰的數字,好比ABC,假設ASCII碼爲,99 100 101 ,url
就是不符合題意的字符串,ACF,就能夠。spa
思路:從相鄰字符的ASCII碼不能是相鄰的數字,能夠想到字符串之間ASCII碼至少差2,而後發現.net
ACE...假設是奇數ASCII碼,BDF是偶數ASCII碼,那麼咱們不妨把他們分紅兩組,code
奇數的字符,偶數的字符,那就簡單了,咱們能夠直接先把奇數的輸出,再把偶數的輸出,那麼字符串blog
之間相鄰字符的ASCII碼不能是相鄰的數字就很好來解決了,下面字符串都轉化爲數字,列舉一些狀況ip
① 只有偶數ci
② 只有奇數字符串
③ 1 3 5 7 2get
④ 1 3 5 7 6
⑤ 1 3 2
⑥ 1 3 2 4
下面四種狀況斷定下就OK了,代碼有呼應。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <queue> 6 #include <map> 7 #include <cmath> 8 #include <iomanip> 9 using namespace std; 10 11 typedef long long LL; 12 #define inf (1LL << 25) 13 #define rep(i,j,k) for(int i = (j); i <= (k); i++) 14 #define rep__(i,j,k) for(int i = (j); i < (k); i++) 15 #define per(i,j,k) for(int i = (j); i >= (k); i--) 16 #define per__(i,j,k) for(int i = (j); i > (k); i--) 17 18 19 int main(){ 20 21 ios::sync_with_stdio(false); 22 cin.tie(0); 23 24 char str[110]; 25 vector<int> one; 26 vector<int> two; 27 28 int T; 29 cin >> T; 30 while(T--){ 31 32 cin >> str; 33 34 one.clear(); //奇數 35 two.clear(); //偶數 36 int len = strlen(str) - 1; 37 rep(i,0,len){ 38 int tmp = str[i] - 'a' + 1; 39 //判奇偶 40 if(tmp & 1) one.push_back(tmp); 41 else two.push_back(tmp); 42 } 43 44 //排好序,方便比較 45 sort(one.begin(),one.end()); 46 sort(two.begin(),two.end()); 47 //只有偶數 48 if(one.size() == 0){ 49 rep(o,0,(int)two.size() - 1) cout << (char)('a' + two[o] - 1); 50 cout << endl; 51 } 52 //只有奇數 53 else if(two.size() == 0){ 54 rep(o,0,(int)one.size() - 1) cout << (char)('a' + one[o] - 1); 55 cout << endl; 56 } 57 else{ 58 int End_b = two.size() - 1; 59 int End_a = one.size() - 1; 60 61 //判斷③④⑤⑥的狀況 62 if(abs(two[0] - one[End_a]) != 1){ 63 rep(o,0,(int)one.size() - 1) cout << (char)('a' + one[o] - 1); 64 rep(o,0,(int)two.size() - 1) cout << (char)('a' + two[o] - 1); 65 cout << endl; 66 } 67 else if(abs(two[End_b] - one[0]) != 1){ 68 rep(o,0,(int)two.size() - 1) cout << (char)('a' + two[o] - 1); 69 rep(o,0,(int)one.size() - 1) cout << (char)('a' + one[o] - 1); 70 cout << endl; 71 } 72 else cout << "No answer" << endl; 73 74 } 75 76 } 77 78 getchar(); getchar(); 79 return 0; 80 }