(manacher 1.1)hdu 3068迴文(使用manacher判斷迴文簡單題)

最長迴文

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 33140    Accepted Submission(s): 12132


 

Problem Description

給出一個只由小寫英文字符a,b,c...y,z組成的字符串S,求S中最長迴文串的長度.
迴文就是正反讀都是一樣的字符串,如aba, abba等

 

 

Input

輸入有多組case,不超過120組,每組輸入爲一行小寫英文字符a,b,c...y,z組成的字符串S
兩組case之間由空行隔開(該空行不用處理)
字符串長度len <= 110000

 

 

Output

每一行一個整數x,對應一組case,表示該組case的字符串中所包含的最長迴文長度.

 

 

Sample Input


 

aaaa abab

 

 

Sample Output


 

4 3

 

 

Source

2009 Multi-University Training Contest 16 - Host by NIT

 

 

Recommend

lcy   |   We have carefully selected several similar problems for you:  3336 3065 3067 3063 3064 

 

#include <vector>
#include <iostream>
#include <string>
#include <cstdio>


using namespace std;

const int MAXN = 100005;


//string Manacher(string s) {
//    // Insert '#'
//    string t = "$#";
//    for (int i = 0; i < s.size(); ++i) {
//        t += s[i];
//        t += "#";
//    }
//
//    // Process t
//    vector<int> p(t.size(), 0);
//
//
//    int mx = 0, id = 0, resLen = 0, resCenter = 0;
//    for (int i = 1; i < t.size(); ++i) {
//
//        //找到迴文串的長度
//        p[i] = mx > i ? min(p[2 * id - i], mx - i) : 1;
//
//        /*
//         * 嘗試拓寬邊界
//         */
//        while (t[i + p[i]] == t[i - p[i]]) {
//            ++p[i];
//        }
//
//        /*
//         * 更新迴文串的邊界
//         */
//        if (mx < i + p[i]) {
//            //更新迴文串右邊的邊界
//            mx = i + p[i];
//            //更新迴文串的中心
//            id = i;
//        }
//
//        //更新迴文串的中心數據
//        if (resLen < p[i]) {
//
//            //迴文串的半徑(就是處理後的迴文串)
//            resLen = p[i];
//            //迴文串的中心
//            resCenter = i;
//
//        }
//    }
//
//    /*
//     * s是原來的文本串.
//     *
//     * resLen:處理後的迴文串的半徑,但是是處理前的迴文串的直徑
//     * resCenter:處理後的迴文串的中心
//     *
//     * resCenter - resLen:計算迴文串的起點
//     * resLen - 1:需要
//     */
//    return s.substr((resCenter - resLen) / 2, resLen - 1);
//}


string manacher(string s) {


    int mx = 0;
    int id = 0;
    int resCenter = 0;
    int resLen = 0;


    string t = "$#";
    for (int i = 0; i < s.length(); ++i) {
        t += s[i];
        t += "#";
    }


    vector<int> p(t.length(), 0);
    for (int i = 0; i < t.length(); ++i) {
        p[i] = mx > i ? min(p[2 * id - i], mx - i) : 1;


        while (t[i + p[i]] == t[i - p[i]]) {
            p[i] += 1;
        }


        if (mx < i + p[i]) {
            mx = i + p[i];
            id = i;
        }

        if (resLen < p[i]) {
            resLen = p[i];
            resCenter = i;
        }

    }


    return s.substr((resCenter - resLen) / 2, resLen - 1);
}


int main() {
//    string s1 = "12212";
//    cout << Manacher(s1) << endl;
//    string s2 = "122122";
//    cout << Manacher(s2) << endl;
//    string s = "waabwswfd";
//    cout << Manacher(s) << endl;
//
//
//    cout << "----------" << endl;
//
//    cout << manacher(s1) << endl;
//    string s22 = "122122";
//    cout << manacher(s22) << endl;
//    string s33 = "waabwswfd";
//    cout << manacher(s33) << endl;

//      string tmp = "";

    char tmp[MAXN];
    while (scanf("%s", tmp) != EOF) {
        string result = manacher(tmp);

        cout << result.length() << endl;

    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章