1208. Legendary Teams Contest
Time limit: 1.0 second Memory limit: 64 MB
Nothing makes as old as years. A lot of cool contests are gone, a lot of programmers are not students anymore and are not allowed to take part at the contests. Though their spirit is fresh and young as it was years ago! And so once they decided to make a contest at the Ural State University among the veteran teams…
To make the contest interesting, they decided to invite as much "legendary" teams as possible. The jury has made a short list of teams, which have shown the best results in the old good times, thus being worthy to hold the name of "legendary". All those teams were invited to take part of the contest, and all of them accepted the invitations. But they have forgotten one important thing at the jury: during the long history of the contests at the university, the teams happened to change and some programmers managed to contest in different "legendary" teams. Though, the jury decided not to give up the initial idea and to form as much legendary teams as possible to participate at the contest — and your program should help the jury!
Input
The first line contains a positive integer K, 1 ≤ K ≤ 18. It is the number of all the legendary teams. There follow the descriptions of the teams in K lines. Each of those lines contains three names of the team members of the respective team. All names are written with not more than 20 small Latin letters.
Output
You should output the maximal possible number of legendary teams of veterans, that could simultaneously participate at the contest.
Sample
input | output |
---|---|
7 gerostratos scorpio shamgshamg zaitsev silverberg cousteau zaitsev petersen shamgshamg clipper petersen shamgshamg clipper bakirelli vasiliadi silverberg atn dolly knuth dijkstra bellman |
4 |
Problem Author: Leonid Volkov Problem Source: USU Internal Contest, March 2002
***********************************************************************************************
字典树加深搜(很笨,很暴力)
***********************************************************************************************
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<string> 3 #include<cstring> 4 #include<cmath> 5 #include<cctype> 6 #include<cstdio> 7 #include<stack> 8 #include<queue> 9 #include<vector> 10 using namespace std; 11 int fn[1000][1000]; 12 int i,j,k,n; 13 int sum,maxsum; 14 bool used[1001]; 15 bool flag[1001]; 16 class nodea//构建一个类(可称之为字典类) 17 { 18 public: 19 int id; 20 nodea *p[131]; 21 nodea() 22 { 23 int i; 24 id=-1; 25 for(i=0;i<=130;i++) 26 p[i]=NULL; 27 } 28 }; 29 nodea *root;//建立指针 30 int ans=1; 31 int getnum(char *s)//扩展树的结点 32 { 33 nodea *r=root; 34 int m=strlen(s); 35 for(int i=0;i<m;i++) 36 { 37 if(r->p[s[i]]==NULL) 38 r->p[s[i]]=new nodea(); 39 r=r->p[s[i]]; 40 } 41 if(r->id==-1) 42 { 43 r->id=ans; 44 ans++; 45 } 46 return r->id;//返回编号 47 } 48 bool check(int x)//判断是否已有队员在别的队 49 { 50 for(int i=1;i<=3;i++) 51 if(used[fn[x][i]]) 52 return false; 53 return true; 54 } 55 void fff(int x,int v)//给本队队员做标记 56 { 57 for(int i=1;i<=3;i++) 58 used[fn[x][i]]=v; 59 } 60 void dfs(int x) 61 { 62 if(sum>maxsum) 63 maxsum=sum; 64 for(int i=x;i<=n;i++) 65 { 66 if(!flag[i]&&check(i)) 67 { 68 sum++; 69 flag[i]=true; 70 fff(i,1); 71 dfs(i); 72 fff(i,0);//还原 73 flag[i]=false; 74 sum--; 75 } 76 } 77 } 78 int main() 79 { 80 char str1[1000],str2[1000],str3[1000]; 81 cin>>n; 82 root=new nodea();//构建对象 83 for(i=1;i<=n;i++) 84 { 85 scanf("%s%s%s",str1,str2,str3); 86 int a=getnum(str1); 87 int b=getnum(str2); 88 int c=getnum(str3); 89 fn[i][1]=a; 90 fn[i][2]=b; 91 fn[i][3]=c; 92 93 } 94 maxsum=1; 95 for(i=1;i<=n;i++) 96 { 97 //注意每次都要初始化 98 memset(used,false,sizeof(used)); 99 memset(flag,false,sizeof(flag)); 100 flag[i]=true; 101 sum=1; 102 for(j=1;j<=3;j++) 103 used[fn[i][j]]=true; 104 dfs(i); 105 } 106 cout<<maxsum<<endl; 107 108 109 }