QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#10197 | #305. 最长公共相似子序列问题 | qwqyz | 10 | 535ms | 23444kb | C++11 | 2.5kb | 2021-05-06 14:48:33 | 2023-08-25 00:54:28 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
inline int read(){
int x=0,y=1;
char c=getchar();
while(c>'9'||c<'0'){
if(c=='-')y=-1;
c=getchar();
}
while(c>='0'&&c<='9'){
x=x*10+c-'0';
c=getchar();
}
return x*y;
}
const int N=2005;
int n,ans,f[N][N],mrk[N][4*N],head=1;
char x[2*N],y[2*N];
void delet(int l,int r,int cnt,int rt){
mrk[rt][cnt]=0;
if(l==r)return;
int mid=(l+r)>>1;
delet(l,mid,cnt*2,rt);
delet(mid+1,r,cnt*2+1,rt);
return;
}
void push(int l,int r,int cnt,int rt,int x){
if(l>x||r<x)return;
if(l==r){
f[rt][x]-=mrk[rt][cnt];
mrk[rt][cnt]=0;
return;
}
int mid=(l+r)>>1;
mrk[rt][cnt*2]+=mrk[rt][cnt];
mrk[rt][cnt*2+1]+=mrk[rt][cnt];
mrk[rt][cnt]=0;
push(l,mid,cnt*2,rt,x);
push(mid+1,r,cnt*2+1,rt,x);
return;
}
void sub(int l,int r,int cnt,int rt,int fl,int fr){
if(l>fr||r<fl)return;
if(l>=fl&&r<=fr){
mrk[rt][cnt]++;
return;
}
int mid=(l+r)>>1;
sub(l,mid,cnt*2,rt,fl,fr);
sub(mid+1,r,cnt*2+1,rt,fl,fr);
return;
}
bool check(int u,int now,int t,int i){
int v=u-1;
if(!v)v=n;
push(1,n,1,v,now);
push(1,n,1,v,now-1);
// push(1,n,1,u,now-1);
push(1,n,1,u,now);
int fl=max(f[u][now-1],f[v][now]);
if(x[i+t]==y[now])fl=max(fl,f[v][now-1]+1);
if(fl<f[u][now])return 0;
else return 1;
}
//void print(){
// for(int i=0;i<n;i++){
// int u=i+head;
// if(u>n)u-=n;
// for(int j=1;j<=n;j++){
// push(1,n,1,u,j);
// cout<<f[u][j]<<' ';
// }
// puts("");
// }
// puts("");
// return;
//}
signed main(){
// freopen("midnight.in","r",stdin);
// freopen("midnight.out","w",stdout);
n=read();
cin>>x+1;
for(int i=1;i<=n;i++)x[i+n]=x[i];
cin>>y+1;
for(int j=1;j<=n;j++){
for(int k=1;k<=n;k++){
f[j][k]=max(f[j-1][k],f[j][k-1]);
if(x[j]==y[k])f[j][k]=max(f[j][k],f[j-1][k-1]+1);
//cout<<f[j][k]<<' ';
}//cout<<'\n';
}//cout<<'\n';
// print();
for(int t=1;t<n;t++){
for(int i=1;i<=n;i++)f[head][i]=0;
delet(1,n,1,head);
head++;
int now=1;
for(int i=1;i<n;i++){
int u=i+head-1;
if(u>n)u-=n;
while(check(u,now,t,i)&&now<=n)now++;
sub(1,n,1,u,now,n);
}
for(int i=1;i<=n;i++){
int u=head+n-1,v=head+n-2;
if(u>n)u-=n;
if(v>n)v-=n;
push(1,n,1,v,i);
push(1,n,1,v,i-1);
// push(1,n,1,u,i-1);
f[u][i]=max(f[v][i],f[u][i-1]);
if(x[t]==y[i])f[u][i]=max(f[u][i],f[v][i-1]+1);
}
// print();
int ls=head+n-1;
if(ls>n)ls-=n;
push(1,n,1,ls,n);
ans=max(ans,f[ls][n]);
}
cout<<ans<<'\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 10
Accepted
time: 1ms
memory: 3532kb
input:
10 TACCAAAAGC ATTCTTCTAT
output:
5
result:
ok 1 number(s): "5"
Test #2:
score: 0
Wrong Answer
time: 37ms
memory: 7508kb
input:
300 AAGGTTCCTGTATTCATTGCATTGCTGAACTACGACCGAGAAATGCAGGCCCGTGGTGAATACATTGCCAGCACACAGTATAAGGTTACCAGCGGTACATGTTTCTAGAAGGCACATTGGGCGGCCCCGACTGGCGGAGACGACGATAACTCTTCGCTCACGTTTACAATGTCTACCGTAGCACTGATGAGGTTTTGTCCCGGGGCTCATAACAAGACTGTAATATACAAACCCACGGTATGCACGGACTACCGATGAGACACACAGCTTACCTGCGCACAAAGCAGTATGGAAAC...
output:
246
result:
wrong answer 1st numbers differ - expected: '197', found: '246'
Test #3:
score: 0
Wrong Answer
time: 41ms
memory: 7420kb
input:
300 AGGAGTTATCGTTAGAGGTTTCCCAATTGGACGCCTAGATTAGGAAGGGCACCGACAACAGCTGTCCTAAAGCTACGGGCGCCTATAATGAACACTGAACGTTGCGCCAGTGTCGCGCCCTACCCTGTCCGCGCCGTAGGATTGTCTCAGTCCTTTATGTAAGACTTGCGAAGTCCGTATCCGACAACATATTAGAAACAGATAAGTCGACGGCGAGCGTTGGAACAATCTATCGCGCCTGCCTTAATCCAGCCTGCGTATAAGGTACACGCCCGTTTGATCGCGTTTGTTTATCG...
output:
245
result:
wrong answer 1st numbers differ - expected: '202', found: '245'
Test #4:
score: 0
Wrong Answer
time: 33ms
memory: 7568kb
input:
300 TCATAAGTTCCTTGTTTAAGAACAAAGATCAAGGTTCTCCTCCCTGCGACAGGCTGTGACCCCGAACACCTATAGCGTCTTGGCGGAGCGGCTGTTTACACAGGGGCGCTGTTCAATCGATCGATCTCTCGCCGAGTCTGAGTAGCTACTCAAGTCCTTGGTTAGGGAGTCCAACAGAGGACCTGTTGTGACAGATCCTGGTGGAGCTATTAACAGAGTTGCACGTCTCCTATCCTCATCCAAAGTCTGCCCGGGTGGATTGCATGCTGTCCCTAGATAGACGACGTCGCCTACGC...
output:
251
result:
wrong answer 1st numbers differ - expected: '199', found: '251'
Test #5:
score: 0
Wrong Answer
time: 535ms
memory: 23276kb
input:
1000 ATTTAGCAGCAGGGAAGTTCAATGGACTCGGGCCGAAACCCGGGTGCGCGTCGCCCGAATACAACGTATCCAGCTGAACAGCCGCCCTACGCACGGTCATGTTGGCACCGGAAGCAATGAATACGGGTCCTAGGTGCCATCGTCACAGGTAAAGATATAGGTCTGACGGGTGGACCTGGGTTTCCCTCGGTGCGATCCCAAGAGCGAATATGCATTGCTTTACGCTCCCTATGTTTAAGCTACGTCCTGACCAAGAGACCCTATCGTTGGTGGCATCGCGAATTACTCTGCTCAT...
output:
849
result:
wrong answer 1st numbers differ - expected: '657', found: '849'
Test #6:
score: 0
Wrong Answer
time: 515ms
memory: 23404kb
input:
1000 TCTGCTTTGTGCCAATATCTGACAAGAACACGCCGTGGCGGGTCTTCATATTCGCCCGGACCCATCACGGTATCTTACTGTAGACCGAGATTTGTACATTTCAACCTATACAGGGTCTGGGACGCACTCGTGACTAAAATTTCGTACTTTAGTCAGCACGGGACGCGAGGAGAACATCAAATAATCCTTGATGCAGGCATCTCATCCTTGTCTCCATCTGCGATAAGAGCAATGGGAACATTCCTCCTAAGCGCCGAGTTAAAACGGACAAAAGCCTCCCCGGTCAGTATTGGTG...
output:
847
result:
wrong answer 1st numbers differ - expected: '653', found: '847'
Test #7:
score: 0
Wrong Answer
time: 528ms
memory: 23444kb
input:
1000 GTGTGGGCGTGGGTAGTGCTATCCTCATCCACGATTTCTGCCGGCTCGTTTGTACGCCGGCGTTCGCAGATATTCCGGGTATCGAACTATCATTGATAATCATACGTAAACGCAAGAGCTGGAGTCTTCTTAGAAGGATACGGATGGTGGGGGGTGCCCTCGTGCCAACGGTTTGTGTCACAACACACGTTTTCGACGTCCTCTGAGGGCATTACTGATTGACGGATCATTCTCGTCAACGGCCAGCGCAGGGGACCTACTTAGGGACTTGCCGATCCTGTAGAACTTCGGATCT...
output:
847
result:
wrong answer 1st numbers differ - expected: '656', found: '847'
Test #8:
score: 0
Time Limit Exceeded
input:
2000 TTCATGCGCTCAGCCTGGCTTGAGGCACACGGAGGTGCGCCGCAAACACCTGTTGACTCAAGGGAGCCGACTGCTTGCTGATTCAAATATAATATCTGGGTGAGTTTAACAGTCTCTGTATTAGCGGTGCGGCCTTGTTCAAAGTTACTCGTGATAACCCCACGCTGAAAGACTTCGTACTTTCCCAAGGTTTTGATAGACTCTGAGGGAACTCGGACCAGCCTTTTTCCCTCTGGCGAGGAGAGGCCGGACTCTTTTTGGGAGGCACCTCTTAGCTTCGCCTTCTACGCACCAA...
output:
result:
Test #9:
score: 0
Time Limit Exceeded
input:
2000 AGTACTCACTAGAGAATCCCCGACCACCCAAGTATTACAAACGAATGGAGATTTCTCTTTGTTACCGCTGCGATGCAGCGGCCACCCGATTTGTTGCTCTGGCCACTATTGGAGTTACAGTGTGGAGCACGCTAGACCGTGGCCTGTGGAAGCGTGCTGCCCCAAACGTAAGAACGAACTTATTCGGCTGCAATTCATCGGGCAGGGCGTTACGGCGTACGAGGGCAAGACAAGGTAGGTCCCGCGGACACACTTGCCTATGCCGTAGGAACATGACAATATGAAGATCGGTACC...
output:
result:
Test #10:
score: 0
Time Limit Exceeded
input:
2000 GACAACCCCCGTCCATCGGCCTCATAATAAGGGGTGCTGATTTGAAAGTTTTTTCCACTACTGCAGTGAAGCTCTTAGCTGCCGTGTTGACCGCCGCCCTACCAAAGCGAATACAAGCTACTAAACTTCGGACAATAGGATTGCATTCCAGCCAGTGAACGTACGGCACTGTCAGAGTTGCCGACTGATGTCGCGTTGAGTTAGGGCCATGGTCTCGATCTAGCAAGACGGTCACTTTCTTATCTTTCGTGACGGCTAATGTATTTTTGTACAGTGGTCTAAGAAACTTAGTTAG...