QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#556655 | #6175. 遗传密码问题 | Matutino | 100 ✓ | 168ms | 139856kb | C++14 | 4.1kb | 2024-09-10 20:00:10 | 2024-09-10 20:00:10 |
Judging History
answer
#include<bits/stdc++.h>
#define reg register
// #define int long long
inline int read(){
reg int x=0,k=1; reg char ch=getchar();
while ('0'>ch||ch>'9') (ch=='-')&&(k=-1),ch=getchar();
while ('0'<=ch&&ch<='9') x=x*10+ch-48,ch=getchar();
return x*k;
}
inline int min(reg int x,reg int y){return x<y?x:y;}
inline bool cmin(reg long long &x,reg long long y){return x>y?x=y,1:0;}
inline bool cmin(reg int &x,reg int y){return x>y?x=y,1:0;}
const int N=3e5+10,D=550;
const long long INF=1e18;
int n,m1,m2,q,ys[300];
long long f[N];
struct Trie{
int ch[N][4],tot,val[N];
inline void clear(){for (reg int i=0;i<=tot;i++) memset(ch[i],0,sizeof(ch[i])); tot=0;}
inline void insert(reg char *S,reg int c){
for (reg int i=1,p=0;S[i];i++){
if (!ch[p][ys[S[i]]]) val[ch[p][ys[S[i]]]=++tot]=c;
p=ch[p][ys[S[i]]],cmin(val[p],c);
}
}
}A,B;
char str[N],S[N],S1[N+N],S2[N+N];
struct SA{
int sa[N+N],x[N+N],y[N+N],c[N+N],rk[N+N],st[20][N+N],h[N+N],m;
inline void build(reg char *s){
m=26; memset(c,0,sizeof(int)*(m+1)); reg int num=0;
for (reg int i=1;i<=m1;i++) c[x[i]=s[i]-'A'+1]++;
for (reg int i=1;i<=m;i++) c[i]+=c[i-1];
for (reg int i=m1;i;i--) sa[c[x[i]]--]=i;
for (reg int k=1;k<=m1;k<<=1){
num=0;for (reg int i=m1;i+k>m1;i--) y[++num]=i;
for (reg int i=1;i<=m1;i++) if (sa[i]>k) y[++num]=sa[i]-k;
memset(c,0,sizeof(int)*(m+1)); for (reg int i=1;i<=m1;i++) c[x[i]]++;
for (reg int i=1;i<=m;i++) c[i]+=c[i-1];
for (reg int i=m1;i;i--) sa[c[x[y[i]]]--]=y[i];
num=0,std::swap(x,y),x[sa[1]]=++num;
for (reg int i=2;i<=m1;i++) x[sa[i]]=(y[sa[i]]==y[sa[i-1]]&&y[sa[i]+k]==y[sa[i-1]+k]?num:++num);
if ((m=num)==m1) break;
}
for (reg int i=1;i<=m1;i++) rk[i]=x[i];
for (reg int i=1,k=0;i<=m1;i++) if (rk[i]>1){
if (k) k--;
while (s[i+k]==s[sa[rk[i]-1]+k]) k++; h[rk[i]]=k;
}else h[rk[i]]=0;
for (reg int i=1;i<=m1;i++) st[0][i]=h[i];
for (reg int j=1;(1<<j)<=m1;j++) for (reg int i=1;i+(1<<j)-1<=m1;i++)
st[j][i]=min(st[j-1][i],st[j-1][i+(1<<j-1)]);
}
inline int LCP(reg int l,reg int r){
l=rk[l],r=rk[r]; if (l>r) std::swap(l,r); l++;// if (l>r) return 0;
reg int len=std::__lg(r-l+1); return min(st[len][l],st[len][r-(1<<len)+1]);
}
inline void clear(){memset(x,0,m1+1<<2),memset(y,0,m1+1<<2);}
}sa1,sa2;
int id[N],tot,len[N],pos[N],cost[N];
inline void work(){
scanf("%s",str+1),q=read(),n=strlen(str+1); m1=m2=tot=0;
for (reg int i=1;i<=n;i++) S1[++m1]=str[i];
for (reg int i=n;i;i--) S2[++m2]=str[i];
for (reg int i=1;i<=q;i++){
scanf("%s",S+1),cost[i]=read(),len[i]=strlen(S+1);
if (len[i]>D){
pos[i]=m1+1;
for (reg int j=1;j<=len[i];j++) S1[++m1]=S[j];
for (reg int j=len[i];j;j--) S2[++m2]=S[j];
id[++tot]=i;
}else{
A.insert(S,cost[i]),std::reverse(S+1,S+len[i]+1),B.insert(S,cost[i]);
}
}
S1[m1+1]=S2[m1+1]=0;
sa1.build(S1),sa2.build(S2),memset(f,0x3f,n+1<<3),f[0]=0;
for (reg int i=0;i<=n;i++){
if (i){
for (reg int j=i,p=0;j;j--){
p=B.ch[p][ys[str[j]]]; if (!p) break;
cmin(f[i],f[j-1]+B.val[p]);
}
for (reg int j=1;j<=tot;j++){
reg int Len=min(min(len[id[j]],i),sa2.LCP(n-i+1,pos[id[j]]));
for (reg int x=1;x<=Len;x++) cmin(f[i],f[i-x]+cost[id[j]]);
}
}
if (i==n) continue;
for (reg int j=1;j<=tot;j++){
reg int Len=min(min(len[id[j]],n-i),sa1.LCP(i+1,pos[id[j]]));
for (reg int x=i+1;x<=i+Len;x++) cmin(f[x],f[i]+cost[id[j]]);
}
for (reg int j=i+1,p=0;j<=n;j++){
p=A.ch[p][ys[str[j]]]; if (!p) break;
cmin(f[j],f[i]+A.val[p]);
}
}
printf("%lld\n",f[n]>INF?-1:f[n]);
A.clear(),B.clear(),sa1.clear(),sa2.clear();
}
signed main(){
ys['A']=0,ys['T']=1,ys['C']=2,ys['G']=3;
for (reg int _=read();_--;) work();
return 0;
}
详细
Pretests
Final Tests
Test #1:
score: 10
Accepted
time: 11ms
memory: 65292kb
input:
10 TGTTCTGAACCACCATGGTGTCCTTGACTAGGGTGCTAAACCAAATCCTGGGTAGCCCGAGAGCAGTACCCGAGCGCAGGGGACGTCCAAA 18 GAAACG 872707675 TACTT 679726598 GTAAA 723748135 TGACG 461452620 TAGACC 719002639 GAGTGT 398114360 CGTGAC 37546213 TGACAT 219353887 CGCGC 465810039 ATGCT 773690110 ATAC 303269549 GCTACC 622915976 TTCTAA...
output:
6765079310 5065843272 6342056657 7741599583 5157680965 12151903110 1245304582 10253088049 8117317044 7062291432
result:
ok 10 numbers
Test #2:
score: 10
Accepted
time: 12ms
memory: 81700kb
input:
10 GGTCTAGAGTATAAAATACTAGTGAAAAGGTCAGACTTCGAGCTGGTGCTCAGAAGGGTTTGATGAAGTGCCAAACTACAATCCAAAGGTTAATTAGTTACACCTAAAGGCAGGACAGTTTTCGGCGGAATAACATTCGCACGTTGAATTATTTGGCTTAGCTTGACAGATAGGTCAGGAGTCACAAAAAGTGGCAGGATTGGATTGCATGTTAAAGCATAAGTGAGCGAGCCGGCACCCACGCGTGTGCTACGTTGCTCCACACAATGATGAGCGCGTTCCAGGAATCTCAACCTG...
output:
149102664725 14898785590 92286311172 130180082392 117423146611 118197799738 124348592127 172913251428 66304188543 56639086523
result:
ok 10 numbers
Test #3:
score: 10
Accepted
time: 18ms
memory: 79708kb
input:
10 AGATTCTAGTCGATCGTCATCATATGCTCGTGGTGTATGCCAACGCCCCCGTGTCCTGGAACATAAACAAACTATCTCGAAACGTCAACGGGATGACTTAACGACCGATCGTGACGATACCAGAAGGGCTATAGACACAGTGACTAATTATCCTACCAAACTTTATGTGCGACGTGACCGTAACGGGATTGCCACTCTTCCTGCCAGGGGTACCCCTTACAGATTGTTGACGCACATTTTATATGGACCTACTTTCAAGAGTGGACATCTTCGGAGATTCCGGACCGGACAATGTCC...
output:
235746326088 123826832039 385120527672 127227771541 181178785515 320080297686 246277451657 22924672297 65068708775 -1
result:
ok 10 numbers
Test #4:
score: 10
Accepted
time: 117ms
memory: 114528kb
input:
10 GTGTTGGCAAAGAATGCAGCAGCTTATTAAGCCGAAAGTGATCAGCGGGGCTAAATGACCGTAATCGGGGCGACTCGAACACACACTCGCGGCCGGCCAAGAACCTGAACCACCGGAAGTTCAGCCTTTCTTCGAGAAATCGTGCATCACATTATAATAACCACCCTCTATGCACTGCGGGGGCCTTACAAGCTCTCGGGCTATCGGAATAGAGTCGTGCACACCATATTAGTGCTCGAACTACTACTTGAAAGCCGCAAGGCAGGCCGCCTATAGAGTTAGTACACAAAACAATAC...
output:
7277903541780 5463135178879 2794921510309 1563143610532 -1 2381532320632 6250347578261 1176229304495 3056675079810 3717182148878
result:
ok 10 numbers
Test #5:
score: 10
Accepted
time: 149ms
memory: 120712kb
input:
5 TTTGCTTTGGCCTTTATGGGCGACCCTATGAGCACAGGCACAACTGAGTTCAGCCAATGCTAGGTGATGCATAGGGTAGTGAGCCTCGCGCATCTTGGGATTACCAACCCAGACCCTGTCTAATGCCATCCATGTCTCATATCTGCTAACCTCCTAAGGCTGCTGCCATCTGTATCAATATTGGTGCTAAACTTAGCTTGAGGCGTGAGGGTCCTATTGTATACGTGATCTAGCCAGTGTAATAACGAGTCAACGTTCATCTGTGGAGAAAGACACTCTTCAATTTGGATGGGCCGGG...
output:
1518565725323 623639116300 2247630224229 1734773661124 1000515142029
result:
ok 5 number(s): "1518565725323 623639116300 224...229 1734773661124 1000515142029"
Test #6:
score: 10
Accepted
time: 168ms
memory: 120540kb
input:
5 TATTCTACTAGCTGAAGTACCGTGCTTCGTGATAATGGTCAGCACACCTTGAAGGCGCCTGTTTGCACAGCCTTGCGTCTGGCGCTTAGTCTCGACCCTGATGCGGAGGATCTTTTCACGATATATATGGTACAGTTATGGTTACTTATTGAGCCCGTATTTGCTATGTCAGTTAAGTCTTGCGTGGCTGTATTCCTCGAGGCGAACAAGTCATTCAACACAACATCAGGGGTGAGGCTTCTCAGCGCCCAGAAAAAACGCTAGCTACGGGGTATCTTAATATAGGCTAGTGAAGGTG...
output:
2321598457480 5424054967674 2118024396266 1195567009829 3400384003090
result:
ok 5 number(s): "2321598457480 5424054967674 21...266 1195567009829 3400384003090"
Test #7:
score: 10
Accepted
time: 51ms
memory: 121292kb
input:
3 GGTTTTCGCGCGTGTCATGAACCAGCATACGCCAGATGGAACACGGTTTAAGGGCGGGAGTTACCTTTAGGCGCATGCTGGACATCTTGAACCCGTTCCTCAGGCGGGGTTGCGAATAAAGAGTTGCAACCCCCTCCACGACCACACGATCAAGAACGATTAATATAATACGAACCCCACTAACAGGAGATCTATCTGGCAGATCTCTCCAATGTGTGACCAGTTCTAGTGGAACCGGGATGATAGACACAATGAAAGGCGGTTGTGACTTTATTAAACAACACAGTCATGTCGTTTG...
output:
355869773343 159763866384 116520030374
result:
ok 3 number(s): "355869773343 159763866384 116520030374"
Test #8:
score: 10
Accepted
time: 66ms
memory: 124924kb
input:
3 TAGCGGGCCTTTTTAAGATACTGGTGAAGGCTCGATCGACGGCTTTAATGGTATTGAAGTCGTTCGCCGTTGTCTTTATAACGCCCCGGATATGGCATCTTATAAGGTCAACAAATTGCACATATAAGCGGCTCCTCCCTTCTCTCAGTCAAGATGACTAAACCGCTGCTTACGAGCAGGAAGTCTTACGAATCCTAGTTATCCCCTACACCTTCTCCTGCTGCCTCAAGACCAGTACGGGTGAGCCATGCAGGAGATTTGAATCTTATTGAAAATAATCGCTGGAACAGAAACAGAT...
output:
432921513168 243242181221 -1
result:
ok 3 number(s): "432921513168 243242181221 -1"
Test #9:
score: 10
Accepted
time: 58ms
memory: 123044kb
input:
3 CAATTCCATGGCATCTGGCCGATAATTGACTCGATATCAGTGATCCAGCCAACTAGTGACGGCAGAGAACCGGACCCACTATGTGCATGTTGTGTGAGCAAGTCCCGTTCCTCGATCGCTTCTAGCAGAGCCGTCTTTAAACTGTATGTCTTCCCGTGCCCCCTAATTAAACCGGCCTTTCTTAACTACCGTTTGCGCAGTATCCGTGAGCGTTAGTGCCCGCAGGATTCTCCCCGAGGGCCCCTGAGCAGGTGATGCCAATGAGGCAAGAGCTCACGCTCGGCACAAAATGAAAGCG...
output:
32444137641 55814939776 -1
result:
ok 3 number(s): "32444137641 55814939776 -1"
Test #10:
score: 10
Accepted
time: 60ms
memory: 139856kb
input:
1 GGGGTATTAGCGTAAGTGTCAACCACTACAGTCAATGCTTACGGTAGAAGGTGGAATAGGACTTACAGCCCCCTCACCAACATGCCTTTTTTAAATTACGGCATCGATTGGGATTGCAATAATGCCCACTGAAGTCCAAGGGAGCGGGAGCGCTCTTTTACGTTGGGCACCATACCGTGTTCGTCAGAACCCCACGAGCTTCGATCTCTGTATATAAGTAATGGGAATCAGCAGGCGACCAGGGTCCGCTTTTGGTTCAAATCAAACGGTCACGTCTACATTGCAGGAGAGAAGTAAA...
output:
161607544918
result:
ok 1 number(s): "161607544918"