UVA的神奇机器
查看原帖
UVA的神奇机器
152980
Euclid楼主2021/7/7 19:16

在本地机和在线IDE的能运行,UVA上RE,求hack数据

#include<bits/stdc++.h>
using namespace std;
string s1,ans;
void mancher()
{
    string res="$#";
    for(int i=0;i<s1.size();i++)
    {
        res+=s1[i];
        res+="#";
    }
    //res+="$";
    //cout<<res<<endl<<"我艹"<<endl;
	int len=res.size();
    vector<int>p(len,0);
    int maxx=-1,id=0;
    int right=0,pos=0;
    for(int i=0;i<len;i++)
    {
        p[i]=right>i?min(p[pos*2-i],right-i):1;
        while(res[i+p[i]]==res[i-p[i]]&&i+p[i]>=0&&i-p[i]<=len)p[i]++;
        if(i+p[i]>right)
        {
            right=i+p[i];
            pos=i;
        }
        if(p[i]>maxx)
        {
            maxx=p[i];
            id=i;
        }
    }
    if(id*2>=len)
    {
        int tot=0;
        for(int i=0;i<id;i++)
        {
            ans[++tot]=res[i];
        }
        ans[++tot]=res[id];
        for(int i=id-1;i>=0;i--)
        {
            ans[++tot]=res[i];
        }
        for(int i=1;i<=tot;i++)
        {
            if(ans[i]!='#'&&ans[i]!='$')
            {
               printf("%c",ans[i]);
            }
        }
        printf("\n");
    }
    else
    {
        int tot=0;
        for(int i=0;i<len-2;i++)
        {
            ans[++tot]=res[i];
        }
        ans[++tot]=res[res.size()-1];
        for(int i=len-1;i>=0;i--)
        {
            ans[++tot]=res[i];
        }
        for(int i=1;i<=tot;i++)
        {
            if(ans[i]!='#'&&ans[i]!='$')
            {
                //cout<<ans[i];
                printf("%c",ans[i]);
            }
        }
		printf("\n");
    }

}
int main()
{
    while(cin>>s1)
    {
        mancher();
    }
    return 0;
}
2021/7/7 19:16
加载中...