Submission #716694

# Submission time Handle Problem Language Result Execution time Memory
716694 2023-03-30T19:58:29 Z amirhoseinfar1385 Necklace (Subtask 1-3) (BOI19_necklace1) C++17
37 / 85
243 ms 71288 KB
#include<bits/stdc++.h>
using namespace std;
string s,ss;
const int maxn=3000+10;
int lcp[maxn][maxn],fkmp[maxn][maxn];
int n,m;
void findlcp(){
	for(int i=0;i<maxn;i++){
		for(int j=0;j<maxn;j++){
			lcp[i][j]=0;
		}
	}
	for(int i=n-1;i>=0;i--){
		for(int j=m-1;j>=0;j--){
			if(s[i]==ss[j]){
				lcp[i][j]=lcp[i+1][j+1]+1;
			}
		}
	}
}

void findkmp(){
	for(int i=0;i<maxn;i++){
		for(int j=0;j<maxn;j++){
			fkmp[i][j]=0;
		}
	}
	for(int i=0;i<n;i++){
		string fake;
		for(int j=i;j<n;j++)
		{
			fake.push_back(s[j]);
		}
		fake.push_back('$');
		fake+=ss;
		int len=n-i;
	//	if(i==5){
	//		cout<<fake<<"\n";
	//	}
		vector<int>kmp(fake.size()+10);
		kmp[0]=-1;
		for(int j=1;j<(int)fake.size();j++){
			int now=kmp[j-1];
			//int cnt=0;
			while(now>=0){
				//cnt++;
				if(fake[j]==fake[now]){
					break;
				}
				if(now==0){
					now=-1;
					break;
				}
				now=kmp[now-1];
			}
			now++;
			kmp[j]=now;
			if(j>len){
				fkmp[i][j-len-1]=now;
			}
		//	cout<<len<<" "<<j<<" "<<kmp[j]<<"\n";
		}
	}
}

pair<int,pair<int,int>> solve(){
	n=s.size(),m=ss.size();
	findlcp();
	findkmp();
	//cout<<lcp[3][4]<<" "<<maxa[3][5]<<"\n";
	pair<int,pair<int,int>>res;
	res.first=0;
	res.second=make_pair(0,0);
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			if(lcp[i][j]>=1){
				res=max(res,make_pair(lcp[i][j],make_pair(i,j)));
				if(j==0){
					continue;
				}
				int lasta=i+lcp[i][j]+fkmp[i+lcp[i][j]][j-1]-1;
		//		if(i==3&&j==4){
		//			cout<<lasta<<"\n";
		//		}

				res=max(res,make_pair(lasta-i+1,make_pair(i,j-1-(lasta-(i+lcp[i][j]-1))+1)));
			}
		}
	}
	return res;
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>s>>ss;
	auto res=solve();
	reverse(ss.begin(),ss.end());
	auto res2=solve();
	//cout<<res2.second.second<<"\n";
	if(res2.first>res.first){
		swap(res2,res);
		res.second.second=m-(res.second.second+res.first-1)-1;
	}
	cout<<res.first<<"\n";
	cout<<res.second.first<<" "<<res.second.second<<"\n";
}
# Verdict Execution time Memory Grader output
1 Correct 35 ms 71172 KB Output is correct
2 Correct 36 ms 71152 KB Output is correct
3 Correct 37 ms 71196 KB Output is correct
4 Correct 37 ms 71180 KB Output is correct
5 Correct 37 ms 71116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 71172 KB Output is correct
2 Correct 36 ms 71152 KB Output is correct
3 Correct 37 ms 71196 KB Output is correct
4 Correct 37 ms 71180 KB Output is correct
5 Correct 37 ms 71116 KB Output is correct
6 Correct 41 ms 71184 KB Output is correct
7 Correct 41 ms 71288 KB Output is correct
8 Partially correct 40 ms 71244 KB Output is partially correct
9 Correct 41 ms 71140 KB Output is correct
10 Correct 39 ms 71124 KB Output is correct
11 Correct 38 ms 71252 KB Output is correct
12 Correct 38 ms 71124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 71172 KB Output is correct
2 Correct 36 ms 71152 KB Output is correct
3 Correct 37 ms 71196 KB Output is correct
4 Correct 37 ms 71180 KB Output is correct
5 Correct 37 ms 71116 KB Output is correct
6 Correct 41 ms 71184 KB Output is correct
7 Correct 41 ms 71288 KB Output is correct
8 Partially correct 40 ms 71244 KB Output is partially correct
9 Correct 41 ms 71140 KB Output is correct
10 Correct 39 ms 71124 KB Output is correct
11 Correct 38 ms 71252 KB Output is correct
12 Correct 38 ms 71124 KB Output is correct
13 Correct 226 ms 71272 KB Output is correct
14 Correct 208 ms 71244 KB Output is correct
15 Partially correct 243 ms 71276 KB Output is partially correct
16 Correct 224 ms 71264 KB Output is correct
17 Correct 190 ms 71232 KB Output is correct
18 Correct 195 ms 71268 KB Output is correct
19 Correct 198 ms 71268 KB Output is correct
20 Correct 210 ms 71248 KB Output is correct
21 Correct 211 ms 71268 KB Output is correct