Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 31871 | Accepted: 12427 |
Descriptionios
Input數組
Outputthis
Sample Inputspa
4 aaaaaaa baaaaaa abaaaaa aabaaaa 0
Sample Outputcode
The highest possible quality is 1/3.
#include <iostream> #include <cstring> #include <cstdio> using namespace std; #define N 2010 #define M 10 char str[N][M];//放字符串 int n;//結點數 int vis[N],dst[N],map[N][N]; //vis訪問數組,dst放各點到MST的最小距離 int finddst(int i,int j)//找兩字符串字符不一樣的列數 { int num=0,k; for (k=0;k<7;k++) { if(str[i][k]!=str[j][k]) { num++; } } return num; } void init() { int j,i; memset(vis,0,sizeof(vis));//訪問數組初始 for (i=0;i<n;i++)//初始化圖 { for (j=0;j<n;j++) { if (i==j) { map[i][j]=0; } map[i][j]=finddst(i,j); } } } void prime() { int i,j,min,point,ans=0; vis[0]=1;//0點放入MST for (i=0;i<n;i++)//dst初始化 { dst[i]=map[i][0]; } for (i=1;i<n;i++) { min=N; for (j=0;j<n;j++)//找距MST最近的點 { if (vis[j]==0&&min>dst[j]) { min=dst[j]; point=j; } } if (min==N)//不連通狀況 { break; } vis[point]=1;//把該點放入MST ans=ans+dst[point]; for (j=0;j<n;j++)//更新各點到MST的最小距離 { if (vis[j]==0&&dst[j]>map[point][j]) { dst[j]=map[point][j]; } } } printf("The highest possible quality is 1/%d.\n",ans); } int main() { int i,j; while (scanf("%d",&n)&&n) { for (i=0;i<n;i++) { scanf("%s",&str[i]); } init(); prime(); } return 0; }