Manacher‘s Algorithm 求最长回文子串

摘自:https://blog.csdn.net/MallowFlower/article/details/87701363
讲解:https://www.cnblogs.com/z360/p/6375514.html

#include <bits/stdc++.h>
using namespace std;
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;//mx为延伸到最右边的位置
    for (int i = 1; i < t.size(); ++i)
    {
        p[i] = mx > i ? min(p[2 * id - i], mx - i) : 1;
        if(i+p[i]>=t.size())
            continue;
        while (t[i + p[i]] == t[i - p[i]])//继续延伸
        {
            ++p[i];
        if (i+p[i]>=t.size())
            break;
        }
        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);//(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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值