Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
贴个超时的,求优化,有高人解答请留言,#include<iostream> using namespace std; int n; char s[2002][8]; int map[2002][2002]; int dis[2002]; bool flag[2002]; const int MAX=10e13; int Find(char *a,char *b){ //比较两个字符串的不同的字符数,这里用的时间最长 int len1=strlen(a); int len2=strlen(b); int i,j; bool a1[10],b1[10]; //比较两个字符串之间有多少个不同的字母 memset(a1,false,sizeof(a1)); memset(b1,false,sizeof(b1)); for(i=0;i<len1;i++) for(j=0;j<len2;j++){ if(a[i]==b[j]&&!a1[i]&&!b1[j]){ a1[i]=true,b1[j]=true; continue; } } int t=0; for(i=0;i<len1;i++) if(!a1[i]) t++; return t; } int prim(){ int tDis=0; int i,j; for(i=0;i<n;i++){ dis[i]=map[0][i]; flag[i]=false; } flag[0]=true; for(i=1;i<n;i++){ int tmp=MAX; int tmpIndex; for(j=0;j<n;j++){ if(!flag[j]&&dis[j]<tmp){ tmp=dis[j]; tmpIndex=j; } } tDis+=tmp; flag[tmpIndex]=true; for(int k=0;k<n;k++){ if(!flag[k]&&dis[j]>map[k][tmpIndex]) dis[k]=map[k][tmpIndex]; } } return tDis; } int main() { // freopen("1.in","r",stdin); // freopen("1.out","w",stdout); while(scanf("%d",&n)!=EOF){ if(n==0) break; int i,j; for(i=0;i<n;i++){ scanf("%s",s[i]); } /* for(i=0;i<n;i++) printf("%s\n",s[i]); */ for(i=0;i<n;i++) for(j=0;j<n;j++) map[i][j]=map[j][i]=Find(s[i],s[j]); printf("The highest possible quality is 1/%d.\n",prim()); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator