1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
#include <iostream> #include <algorithm> #include <queue> #include <vector> #include <cstdlib> #include <cstdio> #include <string> #include <cstring> #include <ctime> #include <iomanip> #include <cmath> #include <set> #include <map> #include <list> #include <stack> using namespace std; inline bool isPali(string &str){ for (int i = 0; i < str.size()/2; i++) { if (str[i] != str[str.size() - 1 - i]) { return false; } } return true; } int main(){ string toProc; while (cin >> toProc) { int cur = 1; for (int i = 1; i < toProc.size(); i++) { cur <<= 1; cur++; } string res; for (; cur > 0; cur--) { string toJudge; int tcur = cur; for (int i = 0; i < toProc.size(); i++) { if (tcur&1) { toJudge += toProc[i]; } tcur >>= 1; } if (isPali(toJudge)) { if (res < toJudge) { res = toJudge; } } } cout << res << endl; } } |
枚举所有的可能性,如果可以的话计数器加一,这个枚举过程可以借助二进制枚举来优化,把每一位当成控制该位对应字符是否使用的开关,通过位运算来判定,就可以了。