zoukankan      html  css  js  c++  java
  • Gym 101350I

    题意

    给一个字符串, 求最长回文镜像子串长度

    思路

    Manacher + 镜像判断
    算法实现 : Manacher算法总结

    AC代码 ( kuangbin板子 )

    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    #define mst(a) memset(a,0,sizeof(a))
    using namespace std;
    
    const int maxn = 1e3+10;
    int len;
    char mrk[] = "A      HI   M O    TUVWXY ";
    char Ma[maxn*2];
    int Mp[maxn*2];
    
    void Manacher( char s[], int len ){
        int l = 0;
        Ma[l++] = '$';
        Ma[l++] = '#';
        for( int i = 0; i < len; i++ ){
            Ma[l++] = s[i];
            Ma[l++] = '#';
        }
        Ma[l] = 0;
        int mx = 0, id = 0;
        for( int i = 0; i < l; i++ ){
            Mp[i] = mx > i ? min(Mp[2*id-i], mx-i) : 1;
            while( Ma[i+Mp[i]] == Ma[i-Mp[i]] && mrk[Ma[i+Mp[i]]-'A'] != ' ' && mrk[Ma[i-Mp[i]]-'A'] != ' ' ) Mp[i]++;
            if( i+Mp[i] > mx ){
                mx = i + Mp[i];
                id = i;
            }
        }
    }
    
    char s[maxn];
    
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--){
            mst(s);
            mst(Ma);
            mst(Mp);
            scanf("%s",s);
            len = strlen(s);
            Manacher(s, len);
            int ans = 0;
            for( int i = 0; i < 2*len+2; i++ )
                if( mrk[Ma[i]-'A'] != ' ' )
                    ans = max(ans, Mp[i]-1);
            printf("%d
    ",ans);
        }
        return 0;
    }
    
  • 相关阅读:
    【Foreign】无聊的计算姬 [Lucas][BSGS]
    【Foreign】远行 [LCT]
    Leetcode题解(十)
    Leetcode题解(九)
    Leetcode题解(八)
    Leetcode题解(七)
    Leetcode题解(六)
    Leetcode题解(五)
    Leetcode题解(四)
    Leetcode题解(三)
  • 原文地址:https://www.cnblogs.com/JinxiSui/p/9740572.html
Copyright © 2011-2022 走看看