Palindrome----Manacher

Palindrome
Time Limit: 15000MS Memory Limit: 65536K
Total Submissions: 8726 Accepted: 3302

Description

Andy the smart computer science student was attending an algorithms class when the professor asked the students a simple question, "Can you propose an efficient algorithm to find the length of the largest palindrome in a string?" 

A string is said to be a palindrome if it reads the same both forwards and backwards, for example "madam" is a palindrome while "acm" is not. 

The students recognized that this is a classical problem but couldn't come up with a solution better than iterating over all substrings and checking whether they are palindrome or not, obviously this algorithm is not efficient at all, after a while Andy raised his hand and said "Okay, I've a better algorithm" and before he starts to explain his idea he stopped for a moment and then said "Well, I've an even better algorithm!". 

If you think you know Andy's final solution then prove it! Given a string of at most 1000000 characters find and print the length of the largest palindrome inside this string.

Input

Your program will be tested on at most 30 test cases, each test case is given as a string of at most 1000000 lowercase characters on a line by itself. The input is terminated by a line that starts with the string "END" (quotes for clarity). 

Output

For each test case in the input print the test case number and the length of the largest palindrome. 

Sample Input

abcbabcbabcba
abacacbaaaab
END

Sample Output

Case 1: 13
Case 2: 6

题目链接:http://poj.org/problem?id=3974


题目的意思是让你求字符串的最长回文串,Manacher算法的模板,详见的上一篇博客。

代码:

#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
using namespace std;
int p[2000000];
void Manacher(string s){
    memset(p,0,sizeof(p));
    int mx=0,id=0;
    for(int i=1;i<=(int)s.size();i++){
        if(mx>i){
            p[i]=(p[2*id-i])<(mx-i)?p[2*id-i]:(mx-i);
        }
        else{
            p[i]=1;
        }
        while(s[i-p[i]]==s[i+p[i]]){
            p[i]++;
        }
        if(i+p[i]>mx){
            mx=i+p[i];
            id=i;
        }
    }
    int Max=0,ii;
    for(int i=1;i<(int)s.size();i++){
        if(p[i]>Max){
            ii=i;
            Max=p[i];
        }
    }
    Max--;
    int ans=0;
    int start=ii-Max;
    int End=ii+Max;
    for(int i=start;i<=End;i++){
        if(s[i]!='#'){
            ans++;
        }
    }
    cout<<ans<<endl;
}
int main(){
    string s;
    int cnt=0;
    while(getline(cin,s)){
        if(s=="END")
            break;
        cnt++;
        int len=s.length();
        string str;
        str+="$#";
        for(int i=0;i<len;i++){
            str+=s[i];
            str+="#";
        }
        printf("Case %d: ",cnt);
        Manacher(str);
    }

    return 0;
}


好的,下面是使用QT实现Manacher算法的代码,同时包括了QT窗口的输入和输出。 首先,我们需要在QT Creator中创建一个新的Qt Widgets项目,然后在mainwindow.cpp文件中添加下面的代码: ``` #include "mainwindow.h" #include "ui_mainwindow.h" #include <QString> #include <QDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(findLongestPalindrome())); } MainWindow::~MainWindow() { delete ui; } void MainWindow::findLongestPalindrome() { QString str = ui->lineEdit->text(); QString result = longestPalindrome(str); ui->label->setText(result); } QString MainWindow::longestPalindrome(QString s) { QString t = "$#"; for (int i = 0; i < s.length(); ++i) { t += s.at(i); t += "#"; } int p[t.length()] = {0}; int mx = 0, id = 0, maxLength = 0, centerIndex = 0; for (int i = 1; i < t.length(); ++i) { p[i] = mx > i ? std::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 (maxLength < p[i]) { maxLength = p[i]; centerIndex = i; } } QString res; for (int i = centerIndex - maxLength + 1; i < centerIndex + maxLength; ++i) { if (t[i] != '#') { res += t[i]; } } return res; } ``` 在这段代码中,我们使用了QT的信号槽机制,将按钮的点击事件连接到了findLongestPalindrome()槽函数。该函数中,首先获取输入框中的字符串,然后调用longestPalindrome()函数来计算最长回文子串,最后将计算结果显示在label标签中。 longestPalindrome()函数实现了Manacher算法,对输入字符串进行预处理,并维护了变量p、mx、id、maxLength和centerIndex,最终返回最长回文子串。 下面是mainwindow.h文件的代码: ``` #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = nullptr); ~MainWindow(); QString longestPalindrome(QString s); public slots: void findLongestPalindrome(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H ``` 最后,在mainwindow.ui文件中添加下面的控件: - QLineEdit:用于输入字符串。 - QPushButton:用于触发计算最长回文子串的按钮。 - QLabel:用于显示计算结果。 然后,将这些控件与对应的槽函数和变量进行连接,就可以运行这个程序了。 注意:为了能够正常使用QString类和std::min函数,需要在mainwindow.cpp文件中添加下面两行代码: ``` #include <QString> #include <algorithm> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值