zoukankan      html  css  js  c++  java
  • hdu3294(manacher)

    传送门:Girls' research

    题意:求最长回文串并输出位置及转换后的字符串。

    分析:manacher算法算出最长回文串后记录中心位置,然后再转换回原字符串的起始和结束位置。

    #pragma comment(linker,"/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <cstring>
    #include <string>
    #include <cmath>
    #include <limits.h>
    #include <iostream>
    #include <algorithm>
    #include <queue>
    #include <cstdlib>
    #include <stack>
    #include <vector>
    #include <set>
    #include <map>
    #define LL long long
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define eps 1e-6
    #define N 200010
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define PII pair<int,int>
    using namespace std;
    inline LL read()
    {
        char ch=getchar();LL x=0,f=1;
        while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
        while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    int p[N<<1],ans,len,num,mx,id,pos;
    char s[N],str[N<<1],ch[10];
    void build()
    {
        len=strlen(s);num=0;
        str[num++]='@';str[num++]='#';
        for(int i=0;i<len;i++)
        {
            str[num++]=s[i];
            str[num++]='#';
        }
        str[num]=0;
    }
    void manacher()
    {
        ans=0;mx=0;
        memset(p,0,sizeof(p));
        for(int i=1;i<num;i++)
        {
            if(mx>i)p[i]=min(p[2*id-i],mx-i);
            else p[i]=1;
            while(str[i-p[i]]==str[i+p[i]])p[i]++;
            if(p[i]+i>mx)mx=p[i]+i,id=i;
            if(ans<p[i]-1)ans=p[i]-1,pos=i;
        }
    }
    char charge(char c)
    {
        return (c-ch[0]+26)%26+'a';
    }
    int main()
    {
        while(scanf("%s%s",ch,s)>0)
        {
            build();
            manacher();
            if(ans<2)puts("No solution!");
            else
            {
                int a,b;
                if(pos%2)
                {
                    a=pos/2-ans/2;
                    b=a+ans-1;
                }
                else
                {
                    a=pos/2-ans/2-1;
                    b=a+ans-1;
                }
                printf("%d %d
    ",a,b);
                for(int i=a;i<=b;i++)printf("%c",charge(s[i]));
                puts("");
            }
        }
    }
    View Code
  • 相关阅读:
    toPrimitive方法使用
    使用js导入Excel数据,转化为json,导出指定json,合并单元格为excel
    vue-router基本使用
    json另类使用
    z-index无效情况
    构造函数另类使用。
    在worker中使用offscreenCanvas
    使用git提交代码一条龙
    IntelliJ IDEA使用技巧一览表
    Android studio 常用快捷键
  • 原文地址:https://www.cnblogs.com/lienus/p/4299659.html
Copyright © 2011-2022 走看看