답안 #657146

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
657146 2022-11-08T23:01:29 Z inksamurai Necklace (Subtask 1-3) (BOI19_necklace1) C++17
85 / 85
337 ms 520 KB
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define rng(i,c,n) for(int i=c;i<n;i++)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3NRqilq ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}

#define all(a) a.begin(),a.end()

vi eval_pf(const string&s){
	int n=sz(s);
	vi pf(n);
	rng(i,1,n){
		int j=pf[i-1];
		while(j and s[i]!=s[j]){
			j=pf[j-1];
		}
		j+=(s[i]==s[j]);
		pf[i]=j;
	}
	return pf;
}

// cp algorithms Z-function and its calculation
vector<int> z_function(const string&s) {
    int n = (int) s.length();
    vector<int> z(n);
    for (int i = 1, l = 0, r = 0; i < n; ++i) {
        if (i <= r)
            z[i] = min (r - i + 1, z[i - l]);
        while (i + z[i] < n && s[z[i]] == s[i + z[i]])
            ++z[i];
        if (i + z[i] - 1 > r)
            l = i, r = i + z[i] - 1;
    }
    return z;
}

signed main(){
_3NRqilq;	
	string s;
	cin>>s;
	string t;
	cin>>t;
	string rt=t;
	reverse(all(rt));
	int n=sz(s);
	int m=sz(t);
	int len=0;
	pii p={0,0};
	rep(_,2){
		rep(i,n)
		{
			vi a(m);
			if(i<n-1){
				string cur=s.substr(i+1,n-i-1);
				cur+=".";
				cur+=rt;
				auto pfa=eval_pf(cur);
				rng(j,n-i,sz(cur)){
					a[m-(j-(n-i))-1]=pfa[j];
				}
			}
			vi b(m);
			if(i){
				string cur=s.substr(0,i);
				reverse(all(cur));
				cur+=".";
				cur+=t;
				auto pfb=z_function(cur);
				rng(j,i+1,sz(cur)){
					b[j-(i+1)]=pfb[j];
				}
			}
			vec(pii) dp(m+1);
			rep(j,m+1){
				dp[j]=pii(j==m?m:(j+a[j]),j);
				if(j){
					dp[j]=max(dp[j],dp[j-1]);
				}
			}
			rep(j,m){
				if(s[i]==t[j]){
					if(j+1<m){
						int frm=j+1;
						int to=frm+b[frm]-1;
						pii q=dp[to+1];
						int now=q.fi-frm+1;
						if(now>len){
							len=now;
							int ni=i-(q.se-frm);
							p={ni,j};
							if(_==1){
								p={n-ni-now,j};
							}
						}
					}
					{
						int now=1;
						if(now>len){
							len=now;
							int ni=i;
							p={ni,j};
							if(_==1){
								p={n-ni-now,j};
							}
						}
					}
				}
			}	
		}
		reverse(all(s));
	}
	cout<<len<<"\n";
	cout<<p.fi<<" "<<p.se<<"\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 8 ms 360 KB Output is correct
7 Correct 6 ms 340 KB Output is correct
8 Correct 7 ms 340 KB Output is correct
9 Correct 8 ms 348 KB Output is correct
10 Correct 6 ms 340 KB Output is correct
11 Correct 6 ms 360 KB Output is correct
12 Correct 6 ms 360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 8 ms 360 KB Output is correct
7 Correct 6 ms 340 KB Output is correct
8 Correct 7 ms 340 KB Output is correct
9 Correct 8 ms 348 KB Output is correct
10 Correct 6 ms 340 KB Output is correct
11 Correct 6 ms 360 KB Output is correct
12 Correct 6 ms 360 KB Output is correct
13 Correct 320 ms 520 KB Output is correct
14 Correct 294 ms 340 KB Output is correct
15 Correct 337 ms 408 KB Output is correct
16 Correct 308 ms 408 KB Output is correct
17 Correct 265 ms 404 KB Output is correct
18 Correct 273 ms 408 KB Output is correct
19 Correct 282 ms 396 KB Output is correct
20 Correct 292 ms 472 KB Output is correct
21 Correct 292 ms 480 KB Output is correct