QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#379328#8571. Palworlducup-team266#TL 957ms56372kbC++142.4kb2024-04-06 17:07:242024-04-06 17:07:25

Judging History

你现在查看的是最新测评结果

  • [2024-04-06 17:07:25]
  • 评测
  • 测评结果:TL
  • 用时:957ms
  • 内存:56372kb
  • [2024-04-06 17:07:24]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
char c[401000],c_[400100];
int p[401000],lg[401000];
namespace opj{
	int rk[1001000],sa[1010000],m,t[1001000],tr[1001000],h[401000],st[401000][20];
	void SA(int n){
		m=128;
		for(int i=1;i<=m;i++)t[i]=0;
		for(int i=1;i<=n;i++)t[rk[i]=c[i]]++;
		for(int i=1;i<=m;i++)t[i]+=t[i-1];
		for(int i=n;i;i--)sa[t[rk[i]]--]=i;
		for(int k=1;;k<<=1){
			int p=0;
			for(int i=n-k+1;i<=n;i++)tr[++p]=i;
			for(int i=1;i<=n;i++)if(sa[i]>k)tr[++p]=sa[i]-k;
			for(int i=1;i<=m;i++)t[i]=0;
			for(int i=1;i<=n;i++)t[rk[i]]++;
			for(int i=1;i<=m;i++)t[i]+=t[i-1];
			for(int i=n;i;i--)sa[t[rk[tr[i]]]--]=tr[i];
			swap(tr,rk),rk[sa[1]]=p=1;
			for(int i=2;i<=n;i++)rk[sa[i]]=(tr[sa[i-1]]==tr[sa[i]]&&tr[sa[i-1]+k]==tr[sa[i]+k])?p:++p;
			if(n==p)break;m=p;
		}
		for(int i=1,k=0;i<=n;i++){
			if(k)k--;int j=sa[rk[i]-1];
			while(i+k<=n&&j+k<=n&&c[i+k]==c[j+k])k++;
			st[rk[i]][0]=k;
		}
		for(int j=1;j<20;j++)for(int i=1;i+(1<<j)-1<=n;i++)
			st[i][j]=min(st[i][j-1],st[i+(1<<(j-1))][j-1]);
	}
	inline int lcp(int x,int y){
		x=rk[x],y=rk[y];if(x>y)swap(x,y);x++;
		int k=lg[y-x+1];
		return min(st[x][k],st[y-(1<<k)+1][k]);
	}
}
int n,m;
int sol(){
	for(int i=1;i<=n;i++)c[n*2+1-i]=c[i];
	opj::SA(n*2);
	c_[0]='?',c_[1]='#';
	for(int i=1;i<=n;i++)c_[i*2]=c[i],c_[i*2+1]='#';
	c_[n*2+2]='!';
	int ans=m+min(n,m);
	for(int i=1,mx=0,id=0;i<=n*2+1;i++){
		if(i<=mx)p[i]=min(mx-i+1,p[2*id-i]);
		while(c_[i+p[i]]==c_[i-p[i]])p[i]++;
		if(i+p[i]-1>mx)mx=i+p[i]-1,id=i;
		ans=max(ans,p[i]-1);
		if(!(i&1)){
			int e=p[i]/2,L=i/2-e,R=i/2+e;
			for(int j=1;j<=m&&R+j<=n+1;j++){
				int lc=0;
				if(L&&R+j<=n)lc=min(opj::lcp(R+j,n*2+1-L),n+1-(R+j));
				ans=max(ans,p[i]-1+(lc+j)*2);
			}
			//ans=max(ans,min(lcp(R+j,),min())+p[i]-1);
		}
		else{
			int e=p[i]/2,L=i/2-e,R=(i+1)/2+e;
			for(int j=1;j<=m&&R+j<=n+1;j++){
				int lc=0;
				if(L&&R+j<=n)lc=min(opj::lcp(R+j,n*2+1-L),n+1-(R+j));
				ans=max(ans,p[i]-1+(lc+j)*2);
			}
		}
	}
	return ans;
}
void soll(){
	scanf("%d%d%s",&n,&m,c+1);
	int ans=sol();
	reverse(c+1,c+n+1);
	ans=max(ans,sol());
	for(int i=2;i<=n;i++)for(int j=i;j<n&&j-i+1<=m;j++)
	ans=max(ans,min(opj::lcp(j+1,n*2+2-i),n-j)*2+j-i+1+m);
	printf("%d\n",ans);
}
int main(){
	lg[0]=-1;
	for(int i=1;i<=400000;i++)lg[i]=lg[i>>1]+1;
	int T;scanf("%d",&T);while(T--)soll();
	return 0;
} 

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 18132kb

input:

4
1 3
a
4 1
icpc
4 2
icpc
8 4
icecream

output:

4
5
5
11

result:

ok 4 number(s): "4 5 5 11"

Test #2:

score: 0
Accepted
time: 748ms
memory: 56372kb

input:

1
200000 66
jsmwjmibgkjvdscqllsxpaxiycmpauhnzschbivtqbjfrxrqvhvfbqecozjewqqpwdfbeqppjkgxhbnniopkptkygspcdswhwadfwhnzovvpshgcdukrupeztkpxwhmctaquqbxtidzbbxsyuaeikuldaoeudletrsmqptaejibkymsjhmwykqsjdvvdaqwelrcpxwrwhuvodipjniowfofbjktkdezwqqbvwsppsmpilntmdmlxgkaxymnugmmcsljkjzjuudnllydwdwwanadynsoiolso...

output:

141

result:

ok 1 number(s): "141"

Test #3:

score: 0
Accepted
time: 957ms
memory: 56284kb

input:

1
200000 100
qhiaajzxinenucrnfffumuhnovpuwcnojbhsktztapgyivmfqrlntwazwnfetwqieckxcnkskpidltiydfoveaucckximydxxfbiwbdufmbhywqkflyqxbijakadqkzftlciccbpnldsqhtjxuxnvkusvizavuyfhdroiuominebadhfqzrpjnzpgyvkejfwmueiltyeqpvwrkanqknyacqganbszktocfuvvsfrboaennwpaabfdnaurvvurysrijnfaonesihbhrrvbvyhpbremsuhhbc...

output:

209

result:

ok 1 number(s): "209"

Test #4:

score: -100
Time Limit Exceeded

input:

10000
21 7
fhcjhdjcfbfbdeeheibhg
18 8
hccgjfaadefhjhcijc
15 7
ahefiiheaigjiid
15 3
fgjjebidbfgbdij
15 5
jccebbgjbhifjhb
20 2
hcbddhibgfccjjcfebcc
19 1
ehbdgiicchijebidbcd
20 8
gbcfghefhbjggecdhcbj
17 2
jjaeaccjbcfiehfdd
23 4
iafjedfbebbhcfgfjbehdaf
22 1
jgiiiaacehcbaiehfggcfi
17 2
jefbbfigfhbhfcici
...

output:

17
21
17
11
13
9
6
18
9
13
7
9
22
11
21
23
9
23
9
16
11
23
25
15
19
13
9
7
21
9
23
13
9
13
17
19
25
23
21
15
23
23
21
11
11
11
21
17
7
24
13
7
21
15
15
9
7
23
25
9
11
15
13
22
15
17
7
15
9
20
17
13
13
15
7
13
23
19
19
21
17
9
23
19
27
15
23
10
17
21
9
23
7
23
21
13
17
7
17
11
13
13
11
15
11
21
15
19...

result: