Cod sursa(job #2518680)

Utilizator cyg_TheoPruteanu Theodor cyg_Theo Data 6 ianuarie 2020 13:36:13
Problema Prefix Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.87 kb
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int SMAX = 1000005;

char s[SMAX];
int z[SMAX];

int main(){
	freopen("prefix.in","r",stdin);
	freopen("prefix.out","w",stdout);
	int tests;
	scanf("%d\n",&tests);
	for(;tests;tests--){
		scanf("%s",&s);
		
		int l=strlen(s);
		int st=0,dr=0;
		for(int k=1;k<l;++k){
			if(k>dr){
				st=dr=k;
				while(dr<l && s[dr]==s[dr-st])
					++dr;
				z[k]=dr-st;
				dr--;
			}else{
				int k1=k-st;
				if(z[k1]<dr-k+1)
					z[k]=z[k1];
				else{
					st=k;
					while(dr<l && s[dr]==s[dr-st])
						++dr;
					z[k]=dr-st;
					dr--;
				}
			}
		}

		int maxs=0;
		for(int i=1;i<l;++i)
			if(z[i]>=i)
				maxs=max(maxs,i+z[i]/i*i);	
	/*
		for(int i=0;i<l;++i)
			printf("%c ",s[i]);
		printf("\n");
		for(int i=0;i<l;++i)
			printf("%d ",z[i]);
		printf("\n");
	*/	
		printf("%d\n",maxs);
	}
	return 0;
}