标签:integer equal ber field color hdu oid space 扩展
地址:http://acm.hdu.edu.cn/showproblem.php?pid=3613
题目:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2326 Accepted Submission(s): 944
思路:扩展kmp
1 #include<iostream>
2 #include<string>
3 #include<cstring>
4 #include<cstdio>
5
6 using namespace std;
7 const int K=1e6+7;
8 int nt[K],extand[K],v[30],sum[K],l[K],r[K];
9 char sa[K],sb[K];
10 void Getnext(char *T,int *next)
11 {
12 int len=strlen(T),a=0;
13 next[0]=len;
14 while(a<len-1 && T[a]==T[a+1]) a++;
15 next[1]=a;
16 a=1;
17 for(int k=2; k<len; k++)
18 {
19 int p=a+next[a]-1,L=next[k-a];
20 if( (k-1)+L >= p)
21 {
22 int j = (p-k+1)>0 ? (p-k+1) : 0;
23 while(k+j<len && T[k+j]==T[j]) j++;
24 next[k]=j;
25 a=k;
26 }
27 else
28 next[k]=L;
29 }
30 }
31 void GetExtand(char *S,char *T,int *next)
32 {
33 Getnext(T,next);
34 int slen=strlen(S),tlen=strlen(T),a=0;
35 int MinLen = slen < tlen ? slen : tlen;
36 while(a<MinLen && S[a]==T[a]) a++;
37 extand[0]=a;
38 a=0;
39 for(int k=1; k<slen; k++)
40 {
41 int p=a+extand[a]-1, L=next[k-a];
42 if( (k-1)+L >= p)
43 {
44 int j= (p-k+1) > 0 ? (p-k+1) : 0;
45 while(k+j<slen && j<tlen && S[k+j]==T[j]) j++;
46 extand[k]=j;
47 a=k;
48 }
49 else
50 extand[k]=L;
51 }
52 }
53 int main(void)
54 {
55 int t;cin>>t;
56 while(t--)
57 {
58 int ans=0;
59 for(int i=0;i<26;i++)
60 scanf("%d",v+i);
61 scanf("%s",sa);
62 int len=strlen(sa);
63 for(int i=0;i<len;i++)
64 sb[i]=sa[len-i-1];
65 sum[0]=v[sa[0]-‘a‘];
66 for(int i=1;i<len;i++)
67 sum[i]=sum[i-1]+v[sa[i]-‘a‘];
68 sb[len]=‘\0‘;
69 GetExtand(sb,sa,nt);
70 for(int i=0;i<len;i++)
71 if(extand[i]==len-i) l[len-i-1]=1;
72 else l[len-i-1]=0;
73 GetExtand(sa,sb,nt);
74 for(int i=0;i<len;i++)
75 if(extand[i]==len-i) r[i]=1;
76 else r[i]=0;
77 for(int i=0;i<len-1;i++)
78 {
79 int tmp=0;
80 if(l[i]) tmp+=sum[i];
81 if(r[i+1]) tmp+=sum[len-1]-sum[i];
82 ans=max(ans,tmp);
83 }
84 printf("%d\n",ans);
85 }
86 return 0;
87 }
标签:integer equal ber field color hdu oid space 扩展
原文地址:http://www.cnblogs.com/weeping/p/6670281.html