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 |
哪位高人帮我看一下,为什么我的是WRONG呢#include <iostream> #include <string> using namespace std; int n; string str[20]; int a[20]; int num[20][20]; int max_num=200; void input() { cin>>n; for(int i=0;i<n;i++) { cin>>str[i]; a[i]=i; } } int match(string str1,string str2) { int num=0; bool flag=true; int j; for(int i=0;i<str1.size()&&flag;i++) { if(str1[i]==str2[0]) { for(j=i;j<str1.size()&&j-i<str2.size();j++) { if(str1[j]!=str2[j-i]) { num=0; break; } num++; } if(j==str1.size()) { flag=false; } } } //cout<<num<<" "; return num; } void fun() { for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { num[i][j]=match(str[i],str[j]); //cout<<num[i][j]; } //cout<<endl; } } //交换 void Swap(int &a,int &b) { int c; c=a; a=b; b=c; } //全排列 void Perm(int k,int m) { if(k==m) { int t=str[a[0]].size();//中间变量 int mid=0; for(int i=1;i<=m;i++) { if(num[a[mid]][a[i]]!=str[a[i]].size()) { mid = i-1; } t=t+str[a[i]].size()-num[a[mid]][a[i]]; if(t>=max_num) { break; } } max_num=(t<max_num)?t:max_num; } else { for(int i=k;i<=m;i++) { Swap(a[k],a[i]); Perm(k+1,m); Swap(a[k],a[i]); } } } void run() { fun(); Perm(0,n-1); } void output() { cout<<max_num<<endl; } int main() { int c; cin>>c; while(c--!=0) { max_num=200; input(); run(); output(); } system("pause"); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator