| 题目链接 | 难度等级 | 完成状态 | 完成分数 | 最后编辑时间 | 需要注意 |
|---|---|---|---|---|---|
| 单词接龙 | ★☆☆☆☆ | 答案正确 | 100 | 2014-10-17 23:27:16 | 无 |
一堆单词,一个字母开始,单词首尾相连(可重复多个字母),每个单词最多用2次,最长能接多长?
暴搜就好了,没什么技巧。
| 1018.cpp代码已折叠
展开折叠内容
|
|---|
#include<iostream>
#include<vector>
using namespace std;
int n,maxLen=0;
vector<string> Str;
vector<int> Used;
void dfs(const char& strId,const int &count,const int &len)
{
maxLen=max(maxLen,len);
for(int i=0;i<n;++i)
{
if(Used[i]>0)
{
Used[i]--;
for(int j=min(Str[strId].size(),Str[i].size())-1;j>=1;--j)
{
string strCut(Str[strId],Str[strId].size()-j,j),
newstrCut(Str[i],0,j);
if(strCut==newstrCut)
{
dfs(i,count+1,len+Str[i].size()-j);
}
}
Used[i]++;
}
}
}
int main()
{
cin>>n;
string str;
for(int i=0;i<n;++i)
{
cin>>str;
Str.push_back(str);
Used.push_back(2);
}
char start;
cin>>start;
for(int i=0;i<n;++i)
{
if(start==Str[i][0])
{
Used[i]--;
dfs(i,1,Str[i].size());
Used[i]++;
}
}
cout<<maxLen<<endl;
return 0;
}
|