标签:des style blog http color java 使用 os
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1610 Accepted Submission(s):
850
#include<stdio.h> #include<string.h> #define inf 1<<27 char str[20]; int dp[65536],len,mark[65536]; int min(int x,int y) { if(x>y) return y; else return x; } int judge(char c[]) { int i,j,n; n=strlen(c); j=n-1; for(i=0;i<=n/2-1&&j>=n/2-1;i++) { if(c[i]!=c[j]) { return 0; } j--; } return 1; } int solve(int x) { char a[20]; int r=0; int y,temp,q=0; temp=x; while(temp) { int cnt=temp%2; if(cnt==1) a[r++]=str[q]; q++; temp=temp/2; } a[r]=‘\0‘; if(judge(a)) return 1; else return 0; } void init() { int i; for(i=1;i<1<<len;i++) { if(solve(i)==1) mark[i]=1; else mark[i]=0; } } int count(int n) { int num = 0; while(n){ n &= (n - 1); num++; } return num; } int main() { int t,i,k,j; scanf("%d",&t); while(t--) { memset(dp,0,sizeof(dp)); memset(mark,0,sizeof(mark)); scanf("%s",str); len=strlen(str); init(); if(judge(str)) { printf("1\n"); continue; } int s=(1<<len)-1; dp[s]=0; for(i =s-1;i>=0;i--) { dp[i] = inf; for(j = i+1;j<=s;j=(j+1)|i)#include<stdio.h> #include<string.h> #define inf 1<<27 char str[20]; int dp[65536],len,mark[65536]; int min(int x,int y) { if(x>y) return y; else return x; } int judge(char c[]) { int i,j,n; n=strlen(c); j=n-1; for(i=0;i<=n/2-1&&j>=n/2-1;i++) { if(c[i]!=c[j]) { return 0; } j--; } return 1; } int solve(int x) { char a[20]; int r=0; int y,temp,q=0; temp=x; while(temp) { int cnt=temp%2; if(cnt==1) a[r++]=str[q]; q++; temp=temp/2; } a[r]=‘\0‘; if(judge(a)) return 1; else return 0; } void init() { int i; for(i=1;i<1<<len;i++) { if(solve(i)==1) mark[i]=1; else mark[i]=0; } } int count(int n) { int num = 0; while(n){ n &= (n - 1); num++; } return num; } int main() { int t,i,k,j; scanf("%d",&t); while(t--) { memset(dp,0,sizeof(dp)); memset(mark,0,sizeof(mark)); scanf("%s",str); len=strlen(str); init(); if(judge(str)) { printf("1\n"); continue; } int s=(1<<len)-1; dp[s]=0; for(i =s-1;i>=0;i--) { dp[i] = inf; for(j = i+1;j<=s;j=(j+1)|i)//#include<stdio.h> #include<string.h> #define inf 1<<27 char str[20]; int dp[65536],len,mark[65536]; int min(int x,int y) { if(x>y) return y; else return x; } int judge(char c[])//判断回文串 { int i,j,n; n=strlen(c); j=n-1; for(i=0;i<=n/2-1&&j>=n/2-1;i++) { if(c[i]!=c[j]) { return 0; } j--; } return 1; } int solve(int x) { char a[20]; int r=0; int y,temp,q=0; temp=x; while(temp) { int cnt=temp%2; if(cnt==1) a[r++]=str[q]; q++; temp=temp/2; } a[r]=‘\0‘; if(judge(a)) return 1; else return 0; } void init() { int i; for(i=1;i<1<<len;i++) { if(solve(i)==1) mark[i]=1; else mark[i]=0; } } int count(int n) { int num = 0; while(n){ n &= (n - 1); num++; } return num; } int main() { int t,i,k,j; scanf("%d",&t); while(t--) { memset(dp,0,sizeof(dp)); memset(mark,0,sizeof(mark)); scanf("%s",str); len=strlen(str); init(); if(judge(str)) { printf("1\n"); continue; } int s=(1<<len)-1; dp[s]=0; for(i =s-1;i>=0;i--) { dp[i] = inf; for(j = i+1;j<=s;j=(j+1)|i)// j = (j+1)|i能省去很多状态 { if(mark[j-i]) { dp[i] = min(dp[i],dp[j]+1); } } } printf("%d\n",dp[0]); } return 0; } { if(mark[j-i]) { dp[i] = min(dp[i],dp[j]+1); } } } printf("%d\n",dp[0]); } return 0; } { if(mark[j-i]) { dp[i] = min(dp[i],dp[j]+1); } } } printf("%d\n",dp[0]); } return 0; }
HDU -4628 Pieces,布布扣,bubuko.com
标签:des style blog http color java 使用 os
原文地址:http://www.cnblogs.com/cancangood/p/3900069.html