码迷,mamicode.com
首页 > 其他好文 > 详细

Ural 1297 Palindrome 【最长回文子串】

时间:2015-01-24 00:29:45      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

最长回文子串 相关资料:

1、暴力法

2、动态规划

3、中心扩展

4、Manacher法 http://blog.csdn.net/ywhorizen/article/details/6629268

http://blog.csdn.net/kangroger/article/details/37742639

 

 

在看后缀数组的时候碰到的这道题目

不过没用后缀数组写出来= = 用了一个很暴力的做法卡进了1 s

 

Source Code:

//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

string a, res, tmp1, tmp2;
int ans;

int main(){
    int i, j, k, t, n, m;
    while(cin >> a){
        ans = 1;
        int len = a.size();
        bool flag = false;
        for(i = 0; i < len; ++i){
            if(len - i < ans)   break;
            for(j = len - i; j >= 1; --j){
                tmp1 = a.substr(i, j);
                tmp2 = tmp1;
                reverse(tmp1.begin(), tmp1.end());a
                if(!flag){
                    if(tmp1.compare(tmp2) == 0 && j >= ans){
                        flag = true;
                        res = tmp1;
                        ans = j;
                        break;
                    }
                } else{
                    if(tmp1.compare(tmp2) == 0 && j > ans){
                        flag = true;
                        res = tmp1;
                        ans = j;
                        break;
                    }
                }

            }
        }
        cout << res << endl;
    }
    return 0;
}

531 ms 是可以过= = 不过大家用后缀数组都是15MS 过的,有点不好意思啊

Ural 1297 Palindrome 【最长回文子串】

标签:

原文地址:http://www.cnblogs.com/wushuaiyi/p/4245272.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!