您的位置:首页 > 其它

2391: Words n个字符串最多能连多长

2010-08-13 18:37 393 查看
2391: Words

ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE

3s8192K53295Standard
Io and Ao are playing a word game. They say a word in the dictionary by turns. The word in the dictionary only contains lowercase letters. And the end character of the former said word should be the same as the start character of the current said word. They can start the game from any word in the dictionary. Any word shouldn't be said twice. Now, we define the complexity of the game that is the sum length of all words said in the game. Give you a dictionary, can you tell me the max complexity of this word game?

Input

The first line contains a single positive integer n(0 < n <=12). Next n lines are n words in the dictionary. The length of each word will not exceed 100.

Output

A single integer represents the complexity of the game.

Sample Input

3
joj
jlu
acm
6
cctv
redcode
lindong
we
love
programming
3
daoyuanlee
come
on

Sample Output

6
11
10


Problem Source: provided by loon

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
using namespace std;
int n,cnt,vis[20];
char a[20][150];
int ok;
void dfs(int id,int now,int num)
{
if(now>cnt) cnt=now;
if(num==n) {ok=1;return ;}
for(int i=0;i<n;i++)
{
if(ok) return ;
if(vis[i]==0&&(num==0||a[id][strlen(a[id])-1]==a[i][0]))
{
vis[i]=1;
dfs(i,now+strlen(a[i]),num+1);
vis[i]=0;
}
}
}
int main()
{
while(scanf("%d",&n)==1)
{
cnt=0;ok=0;
for(int i=0;i<n;i++) scanf("%s",a[i]);
memset(vis,0,sizeof(vis));
dfs(0,0,0);
printf("%d/n",cnt);
}
return 0;
}
/*
4
abbc
abc
ba
cg
8
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐