답안 #716711

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
716711 2023-03-30T22:09:14 Z amirhoseinfar1385 Necklace (Subtask 1-3) (BOI19_necklace1) C++17
85 / 85
406 ms 384 KB
#include<bits/stdc++.h>
using namespace std;
string s,ss;
int n,m;
const int maxn=3000+10;
int lcp[maxn],fkmp[maxn],revfkmp[maxn];
string fake;
vector<int>kmp;

void findkmp(int mid){
	for(int i=0;i<maxn;i++){
		revfkmp[i]=fkmp[i]=0;
	}
	fake.clear();
	for(int i=mid;i<n;i++){
		fake.push_back(s[i]);
	}
	fake.push_back('$');
	int len=fake.size();
	fake+=ss;
	kmp.clear();
	kmp.resize((int)fake.size()+10);
	for(int i=1;i<(int)fake.size();i++){
		int now=kmp[i-1];
		while(now>=0){
			if(fake[i]==fake[now]){
				break;
			}
			if(now==0){
				now=-1;
				break;
			}
			now=kmp[now-1];
		}
		now++;
		kmp[i]=now;
		if(i>=len){
			fkmp[i-len]=now;
		}
	}
	fake.clear();
	for(int i=mid-1;i>=0;i--){
		fake.push_back(s[i]);
	}
	fake.push_back('$');
	len=fake.size();
	reverse(ss.begin(),ss.end());
	fake+=ss;
	reverse(ss.begin(),ss.end());
	kmp.clear();
	kmp.resize((int)fake.size()+10);
	for(int i=1;i<(int)fake.size();i++){
		int now=kmp[i-1];
		while(now>=0){
			if(fake[now]==fake[i]){
				break;
			}
			if(now==0){
				now=-1;
				break;
			}
			now=kmp[now-1];
		}
		now++;
		kmp[i]=now;
		if(i>=len){
			revfkmp[m-1-(i-len)]=now;
		}
	}

}

pair<int,pair<int,int>> solve(){
	n=s.size(),m=ss.size();
	pair<int,pair<int,int>>res;
	for(int i=0;i<maxn;i++){
		lcp[i]=0;
	}
	for(int i=n-1;i>=0;i--){
		for(int j=0;j<m;j++){
			if(s[i]==ss[j]){
				lcp[j]=lcp[j+1]+1;
			}
			else{
				lcp[j]=0;
			}
			//cout<<i<<" "<<j<<" "<<lcp[j]<<"\n";
			if(res.first<lcp[j]){
				res=make_pair(lcp[j],make_pair(i,j));
			}
		}
	}
	for(int i=1;i<n;i++){
		findkmp(i);
		for(int h=1;h<m;h++){
//			if(i==3){
//				cout<<h<<" "<<fkmp[h-1]<<" "<<revfkmp[h]<<"\n";
//			}
			if(fkmp[h-1]+revfkmp[h]>res.first){
				//cout<<"wtf "<<fkmp[h-1]<<" "<<revfkmp[h]<<" "<<fkmp[h-1]+revfkmp[h]<<" "<<i<<" "<<h<<"\n";
				res=make_pair(fkmp[h-1]+revfkmp[h],make_pair(i-revfkmp[h],h-fkmp[h-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";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 8 ms 356 KB Output is correct
7 Correct 7 ms 356 KB Output is correct
8 Correct 8 ms 340 KB Output is correct
9 Correct 8 ms 340 KB Output is correct
10 Correct 7 ms 340 KB Output is correct
11 Correct 8 ms 340 KB Output is correct
12 Correct 7 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 8 ms 356 KB Output is correct
7 Correct 7 ms 356 KB Output is correct
8 Correct 8 ms 340 KB Output is correct
9 Correct 8 ms 340 KB Output is correct
10 Correct 7 ms 340 KB Output is correct
11 Correct 8 ms 340 KB Output is correct
12 Correct 7 ms 340 KB Output is correct
13 Correct 397 ms 380 KB Output is correct
14 Correct 359 ms 380 KB Output is correct
15 Correct 406 ms 384 KB Output is correct
16 Correct 379 ms 380 KB Output is correct
17 Correct 317 ms 376 KB Output is correct
18 Correct 328 ms 376 KB Output is correct
19 Correct 347 ms 380 KB Output is correct
20 Correct 362 ms 376 KB Output is correct
21 Correct 375 ms 340 KB Output is correct