問題:ios
連接:http://poj.org/problem?id=3617spa
思路:code
按照字典序比較S和將S反轉後的字符串S'blog
若是S較小,就從S的開頭取出一個字符,加到T的末尾(更新下標值)ci
若是S’較小,從S’的末尾取出一個字符,加到T的末尾(更新下標值)字符串
代碼:get
# include <iostream> # include <string> using namespace std; int N; char str[2001]; char toStr[2001]; int main() { cin>>N; for(int i = 0; i < N; i++) { cin>>str[i]; } int left = 0; int right = N - 1; int icount = 0; while(left < right) { int leftVar = left; int rightVar = right; while(str[leftVar] == str[rightVar]) { leftVar++; rightVar--; } if (str[leftVar] < str[rightVar]) { toStr[icount++] = str[left]; left++; } else { toStr[icount++] = str[right]; right--; } } toStr[icount++] = str[left]; for(int i = 0; i < icount; i++) { cout<<toStr[i]; if( (i+1) % 80 == 0 && i < icount - 1) cout<<endl; } return 0; }