043_字典序最小問題(best cow line)
阿新 • • 發佈:2019-02-14
poj 3617
從一個字串s中頭部或者尾部取同一個字元,加到字串T中,要求字串T的字典序最小。
對s和s的逆序s'進行比較。如果一樣再對下一對進行比較,保證較小的字母較早被訪問。
題源來自《挑戰程式競賽》第二版 43頁。
// // 043_best cow line.cpp // changlle // // Created by user on 12/26/15. // Copyright (c) 2015 user. All rights reserved. // #include <iostream> #include <string> using namespace std; int N=6; char s[]="ACDBCB"; int main () { char *a=s; char *b=a+N-1; //cout<<*a<<endl; char t[100]; for (int i=0; i<N;i++){ bool left=false; for (int j=0; (a+j)<=(b-j); j++) { if (*(a+j)<*(b-j)) { left=true; break; } if (*(a+j)>*(b-j)) { left=false; break; } } if (left==true) { t[i]=*a; a++; } else { t[i]=*b; b--; } } for (int i=0;i<N;i++) { cout<<t[i]; } return 0; }