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

hdu 5340 Three Palindromes 【Manacher】

时间:2015-08-02 13:46:51      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5340

题意:判断一个字符串能否分为三个回文串

解法:manacher枚举第一第三个,判断第二个。

代码:

#include <stdio.h>
#include <ctime>
#include <math.h>
#include <limits.h>
#include <complex>
#include <string>
#include <functional>
#include <iterator>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <bitset>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <ctime>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <time.h>
#include <ctype.h>
#include <string.h>
#include <string>
#include <assert.h>

using namespace std;

int t;
int p[220100];
char s[220100], c[221000];
int pre[221000], last[221000];

int solve()
{
    int len = strlen(s);
    c[0] = ‘$‘;
    for (int i = 0; i<len; i++)
        c[i * 2 + 1] = ‘#‘, c[i * 2 + 2] = s[i];
    c[len * 2 + 1] = ‘#‘;
    len = len * 2 + 2;
    c[len] = 0;
    int mx = 0, id = 0;
    for (int i = 1; i<len; i++)
    {
        if (mx>i) p[i] = min(p[id * 2 - i], p[id] + id - i);
        else p[i] = 1;
        while (c[i + p[i]] == c[i - p[i]]) p[i]++;
        if (i + p[i]>mx) mx = i + p[i], id = i;
    }

    int p_num = 0, l_num = 0;
    for (int i = 2; i < len - 1; i++)
    {
        if (i == p[i]) pre[p_num++] = i;
        if (i + p[i] == len) last[l_num++] = i;
    }

    for (int i = 0; i < p_num; i++)
    {
        for (int j = l_num - 1; j >= 0; j--)
        {
            int pos1 = pre[i] + p[pre[i]];
            int pos2 = last[j] - p[last[j]];
            if (pos1 > pos2) break;
            int mid = (pos1 + pos2) / 2;
            if (mid<=len-4)
            if (mid - pos1 + 1  <= p[mid]) return 1;
        }
    }
    return 0;
}

int main()
{
    scanf("%d", &t);
    while (t--)
    {
        scanf("%s", s);
        if (solve()) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}

版权声明:转载请注明出处。

hdu 5340 Three Palindromes 【Manacher】

标签:

原文地址:http://blog.csdn.net/u014427196/article/details/47205883

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