Best Cow Line(POJ No.3617)
问题:
链接:http://poj.org/problem?id=3617
思路:
按照字典序比较S和将S反转后的字符串S'
如果S较小,就从S的开头取出一个字符,加到T的末尾(更新下标值)
如果S’较小,从S’的末尾取出一个字符,加到T的末尾(更新下标值)
代码:
# 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; }
作者:viczzx 出处:http://www.cnblogs.com/zixuan-zhang 欢迎转载,也请保留这段声明。谢谢!