[hackerrank]Palindrome Index

简单题。

#include <iostream>
#include <string>
using namespace std;

int main() {
    int T;
    cin >> T;
    while (T--) {
        string s;
        cin >> s;
        int l = 0;
        int r = s.size() - 1;
        while (l < r && s[l] == s[r]) {
            l++;
            r--;
        }
        if (l >= r) {
            cout << -1 << endl;
            continue;
        }
        int ll = l + 1;
        int rr = r;
        while (ll < rr && s[ll] == s[rr]) {
            ll++;
            rr--;
        }
        if (ll >= rr) {
            cout << l << endl;
        } else {
            cout << r << endl;
        }
    }
    return 0;
}

  

posted @ 2014-08-14 00:08  阿牧遥  阅读(217)  评论(0编辑  收藏  举报