TLE,不知道为什么
查看原帖
TLE,不知道为什么
610889
I_love_maths楼主2024/8/24 11:07
#include<bits/stdc++.h>
using namespace std;
string s,t;
int n,m,a[200005];
int main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	while(cin>>s){
		n=s.length(),m=2*n+1;
		t=" ";
		for(int i=0;i<n;i++) t=t+s[i]+" ";
		int l=0,r=0,x,ans=0;
		for(int i=0;i<m;i++){
			if(i>r) x=1;
			else x=min(a[l+r-i],r-i+1);
			while(i-x>=0&&i+x<m&&t[i-x]==t[i+x]) x++;
			a[i]=x;
			if(i+x==m) ans=max(ans,x-1);
			if(i+x-1>r) l=i-x+1,r=i+x-1;
		}
		for(int i=n-ans-1;i>=0;i--) s.push_back(s[i]);
		cout<<s<<"\n";
	}
	return 0;
}
2024/8/24 11:07
加载中...