• hdu 5371 Hotaru's problem【manacher】


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

    pid=5371

    题意:
    给出一个长度为n的串,要求找出一条最长连续子串。这个子串要满足:1:能够平均分成三段,2:第一段和第三段相等,3:第一段和第二段回文。求最大子串的长度。

    代码:

    #include<stdio.h>
    #include<iostream>
    #include<math.h>
    #include<stdlib.h>
    #include<ctype.h>
    #include<algorithm>
    #include<vector>
    #include<string>
    #include<queue>
    #include<stack>
    #include<set>
    #include<map>
    #include<string.h>
    
    using namespace std;
    
    const int N = 1100550;
    
    int n, l, tmp;
    int p[2 * N];//记录回文半径
    int str0[N];//原始串
    int str[2 * N];//转换后的串
    
    void init()
    {
        int i;
        str[0] = -2; str[1] = -1;
        l = 2;
        for (i = 0, l = 2; i<n; i++, l += 2)
        {
            str[l] = str0[i];
            str[l + 1] = -1;
        }
        str[l] = -5;
    }
    
    int solve()
    {
        int i, mx, id;
        mx = 0;//mx即为当前计算回文串最右边字符的最大值  
        for (i = 1; i < l; i++)
        {
            if (mx>i)
                p[i] = p[2 * id - i]>(mx - i) ? (mx - i) : p[2 * id - i];
            else
                p[i] = 1;//假设i>=mx,要从头開始匹配  
            while (str[i + p[i]] == str[i - p[i]])
                p[i]++;
            if (i + p[i]>mx)//若新计算的回文串右端点位置大于mx。要更新po和mx的值
            {
                mx = i + p[i];
                id = i;
            }
        }
    
        int ans = 0;
        for (int i = 1; i < l; i += 2)
            for (int j = i + p[i] - 1; j - i > ans; j -= 2)
            {
                if (p[j] >= j-i+1 && ans < j - i)
                {
                    ans = j - i;
                    break;
                }
            }
    
        return ans / 2 * 3;
    }
    int main()
    {
        int t;
        int cases = 1;
        scanf("%d", &t);
        while (t--)
        {
            scanf("%d", &n);
    
            for (int i = 0; i < n; i++)
                scanf("%d",&str0[i]);
            init();
            printf("Case #%d: %d
    ", cases++, solve());
        }
        return 0;
    }
  • 相关阅读:
    Android NDK Downloads
    Download Blackarch Linux
    Download Kali Linux
    Download ubuntu Linux
    cocos2D-X 常见49种Action
    win10 快速访问存在 2345Downloads 删除解决方案
    C++ 短信验证码/通知
    windows 登陆服务器
    使用路由器的虚拟服务器
    C++:查找字符串字串并替换
  • 原文地址:https://www.cnblogs.com/yangykaifa/p/7243020.html
Copyright © 2020-2023  润新知