标签:oid integer mem 分解 color ems lin tin 循环
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Problem Description
ztr love reserach substring.Today ,he has n string.Now ztr want to konw,can he take out exactly k palindrome from all substring of these n string,and thrn sum of length of these k substring is L.for example string "yjqqaq".this string contains plalindromes:"y","j","q","a","q","qq","qaq".so we can choose "qq" and "qaq".
1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 const int N=110; 5 int n,t,m,k,ct,mx,l; 6 int r[N<<1],f[N][N],vis[N]; 7 char s[N<<1],str[N]; 8 inline int max(int a,int b){return a>b?a:b;} 9 inline int min(int a,int b){return a<b?a:b;} 10 inline void manacher() 11 { 12 memset(s,0,sizeof(s)); 13 n=0;m=strlen(str); 14 s[n++]=1;s[n++]=2; 15 for(int i=0;i<m;i++)s[n++]=str[i],s[n++]=2; 16 memset(r,0,sizeof(r));ct=mx=0; 17 for(int i=0;i<n;i++) 18 { 19 if(i<mx)r[i]=min(mx-i,r[2*ct-i]); 20 else r[i]=1; 21 while(0<=i-r[i]&&i+r[i]<n&&s[i-r[i]]==s[i+r[i]])r[i]++; 22 if(i+r[i]>mx)ct=i,mx=i+r[i]; 23 int j=r[i]-1; 24 while(j>=0)vis[j]++,j-=2; 25 } 26 } 27 int c[N*N],v[N*N],cnt; 28 inline bool backpack() 29 { 30 memset(f,0,sizeof(f)); 31 for(int i=1;i<=l;i++) 32 { 33 int tmp=1; 34 while(vis[i]>=tmp) 35 c[++cnt]=tmp*i,v[cnt]=tmp,vis[i]-=tmp,tmp<<=1; 36 if(vis[i]) 37 c[++cnt]=vis[i]*i,v[cnt]=vis[i]; 38 } 39 f[0][0]=1; 40 for(int u=1;u<=cnt;u++) 41 for(int i=l;i>=c[u];i--) 42 for(int j=k;j>=v[u];j--) 43 f[i][j]|=f[i-c[u]][j-v[u]]; 44 return f[l][k]; 45 } 46 int main() 47 { 48 int cnt;scanf("%d",&cnt); 49 while(cnt--) 50 { 51 scanf("%d%d%d",&t,&k,&l); 52 memset(vis,0,sizeof(vis)); 53 for(int i=1;i<=t;i++) 54 scanf("%s",str),manacher(); 55 if(backpack())printf("True\n"); 56 else printf("False\n"); 57 } 58 }
标签:oid integer mem 分解 color ems lin tin 循环
原文地址:http://www.cnblogs.com/LadyLex/p/7143207.html