2391: Words n个字符串最多能连多长
2010-08-13 18:37
393 查看
2391: Words
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?
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
*/
Result | TIME Limit | MEMORY Limit | Run Times | AC Times | JUDGE |
---|---|---|---|---|---|
3s | 8192K | 532 | 95 | Standard |
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
*/
相关文章推荐
- PHP 有n 个长为m+1 的字符串, 如果某个字符串的最后m 个字符与某个字符串的前m 个字符匹配,则两个字符串可以联接, 问这n 个字符串最多可以连成一个多长的字符串,如果出现循环,则返回错误
- 有n个长为m+1的字符串, 如果某个字符串的最后m个字符与某个字符串的前m个字符匹配,则两个字符串可以联接, 问这n个字符串最多可以连成一个多长的字符串,如果出现循环,则返回错误。
- 有n 个长为m+1 的字符串,如果某个字符串的最后m 个字符与某个字符串的前m 个字符 匹配,则两个字符串可以联接,问这n 个字符串最多可以连成一个多长的字符串
- 有n个长为m+1的字符串,如果某个字符串的最后m个字符与某个字符串的前m个字符匹配,则两个字符串可以联接,问这n个字符串最多可以连成一个多长的字符串,如果出现循环,则返回错误。
- 有n 个长为m+1 的字符串,如果某个字符串的最后m 个字符与某个字符串的前m 个字符匹配,则两个字符串可以联接,问这n 个字符串最多可以连成一个多长的字符串,如果出现循环,则返回错误
- php中计算未知长度的字符串哪个字符出现的次数最多的代码
- 求一个字符串中连续出现次数最多的字串
- 查找字符串中出现次数最多的字符
- 求一个字符串中连续出现次数最多的子串
- Substring with Concatenation of All Words 字符串操作 匹配的字串数
- 求字符串中出现次数最多的字符及其出现次数
- 寻找一个字符串中连续出现次数最多的子串
- 【字符串·Trie】uva3942 Remember the words
- 求字符串中字符最多的字符
- POJ 2817 状态DP 字符串找最多的重复
- 求字符串中出现次数最多和第二多的字母
- 判断一个字符串中出现次数最多的字符,统计这个次数
- 利用JavaScript获取字符串中出现次数最多的字符
- 统计一串字符串中出现次数最多和次多的单词(华为上机考试题)
- 寻找一个字符串出现次数最多的字母以及次数