单词接龙
 单词接龙是一个与我们经常玩的成语接龙相类似的游戏,现在我们已知一组单词,且给定一个开头的字母,要求出以这个字母开头的最长的“龙”(每个单词都最多在“龙”中出现两次),在两个单词相连时,其重合部分合为一部分,例如 beast和astonish,如果接成一条龙则变为beastonish,另外相邻的两部分不能存在包含关系,例如at 和 atide 间不能相连。
Input
 有多测测试数据。

对于每组测试数据,第一行为一个单独的整数n (n<=20)表示单词数,以下n 行每行有一个单词,输入的最后一行为一个单个字符,表示“龙”开头的字母。你可以假定以此字母开头的“龙”一定存在.
Output
只需输出以此字母开头的最长的“龙”的长度
Sample Input
5

at

touch

cheat

choose

tact

a
Sample Output
23
Hint
样例中连成的“龙”atoucheatactactouchoose。
——————————————————————————————————————

dfs,根据单词重叠度建图,边权为重叠个数,从源点搜索,记录最大值
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <set>
#include <map>
using namespace std; #define inf 0x3f3f3f3f int edge[25][25];
char s[25][10005];
int val[25];
int vis[25];
int mx;
int n; void dfs(int num,int len)
{
mx=max(mx,len);
for(int i=0; i<n; i++)
{
if(vis[i]<2&&edge[num][i]!=inf)
{
vis[i]++;
dfs(i,len+val[i]-edge[num][i]);
vis[i]--;
}
}
} int main()
{ while(~scanf("%d",&n))
{
for(int i=0; i<=n; i++)
scanf("%s",&s[i]);
for(int i=0; i<n; i++)
{
val[i]=strlen(s[i]);
}
memset(edge,inf,sizeof edge);
memset(vis,0,sizeof vis);
for(int i=0; i<n; i++)
{
if(s[i][0]==s[n][0])
edge[n][i]=0;
}
for(int i=0; i<n; i++)
{ for(int j=0; j<n; j++)
{
int a=strlen(s[i]);
int b=strlen(s[j]);
int l=1;
int r=0;
while(l<a)
{
r=0;
int fla=1;
if(s[i][l]==s[j][r])
{
if(l+b<a)
{
l++;
continue;
} for(; l+r<a; r++)
{
if(s[i][l+r]!=s[j][r])
{
fla=0;
}
}
if(fla==1)
edge[i][j]=min(edge[i][j],r);
} l++;
} } }
for(int i=0; i<n; i++)
{
if(s[i][0]==s[n][0])
edge[n][i]=0;
} /* for(int i=0; i<=n; i++)
{
for(int j=0; j<=n; j++)
{
printf("%d ",edge[i][j]);
}
printf("\n");
}*/
mx=0;
dfs(n,0);
printf("%d\n",mx);
} return 0;
}


HRBUST1213 单词接龙 2017-03-06 15:53 67人阅读 评论(0) 收藏的更多相关文章

  1. 搜索 基础 AC 2014-01-14 15:53 170人阅读 评论(0) 收藏

    题目网址:http://haut.openjudge.cn/xiyoulianxi1/1/ 1:晶矿的个数 查看 提交 统计 提问 总时间限制:  1000ms  内存限制:  65536kB 描述 ...

  2. Basic 分类: POJ 2015-08-03 15:49 3人阅读 评论(0) 收藏

    Basic Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 905 Accepted: 228 Description The p ...

  3. Gold Coins 分类: POJ 2015-06-10 15:04 16人阅读 评论(0) 收藏

    Gold Coins Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21767   Accepted: 13641 Desc ...

  4. PIE(二分) 分类: 二分查找 2015-06-07 15:46 9人阅读 评论(0) 收藏

    Pie Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submissio ...

  5. 苹果应用商店AppStore审核中文指南 分类: ios相关 app相关 2015-07-27 15:33 84人阅读 评论(0) 收藏

    目录 1. 条款与条件 2. 功能 3. 元数据.评级与排名 4. 位置 5. 推送通知 6. 游戏中心 7. 广告 8. 商标与商业外观 9. 媒体内容 10. 用户界面 11. 购买与货币 12. ...

  6. Codeforces816A Karen and Morning 2017-06-27 15:11 43人阅读 评论(0) 收藏

    A. Karen and Morning time limit per test 2 seconds memory limit per test 512 megabytes input standar ...

  7. Codeforces777D Cloud of Hashtags 2017-05-04 18:06 67人阅读 评论(0) 收藏

    D. Cloud of Hashtags time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  8. hdu1158 Employment Planning 2016-09-11 15:14 33人阅读 评论(0) 收藏

    Employment Planning Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  9. 初学者必知的Python中优雅的用法 分类: Python 2015-05-11 15:02 782人阅读 评论(0) 收藏

    本文由 伯乐在线 - 淘小米 翻译,Daetalus 校稿.未经许可,禁止转载!英文出处:freepythontips.wordpress.com.欢迎加入翻译小组. Hi 朋友们.由于我最近都比较忙 ...

随机推荐

  1. (判断)window.open()窗口被关闭后执行事件

    $(function() { // start ready var $article_share=$('#body .article').find('li.share'); // $article_s ...

  2. Ubuntu 16.04 natural scrolling

    http://ubuntuhandbook.org/index.php/2016/05/install-ubuntu-tweak-in-ubuntu-16-04/ download ubuntu-tw ...

  3. 黄聪:如何配置Emeditor实现代码智能识别自动完成功能

    设置方法如图所示: 效果如下图所示:

  4. 黄聪:js 获取浏览器、Body、滚动条、可见区域、页面、边框、窗口高度和宽度值(多浏览器)

    IE中:document.body.clientWidth ==> BODY对象宽度document.body.clientHeight ==> BODY对象高度document.docu ...

  5. TCP heart

    http://blog.csdn.net/lisonglisonglisong/article/details/51327695

  6. 转转转---ROWNUMBER() OVER( PARTITION BY COL1 ORDER BY COL2)用法

    ROWNUMBER() OVER( PARTITION BY COL1 ORDER BY COL2)用法   http://blog.csdn.net/yinshan33/article/detail ...

  7. 自动手动随便你 Win7驱动程序安装自己设

    Win7系统是非常智能方便的操作系统,可以自动安装硬件驱动程序,为用户提供了很多方便.但是并不是所有的驱动程序和硬件都能完美兼容,如果不合适就需要卸载了重新安装:还有一些朋友就习惯自己安装驱动,那么, ...

  8. phpstorm破解 IntelliJ IDEA License Server本地搭建教程 http://blog.lanyus.com/archives/174.html/comment-page-6#comments 附件:mac环境

    ilanyu's Blog 搜索关键字搜索 首页IDEA注册码文件中转在线记事本老博客关于 IntelliJ IDEA License Server本地搭建教程 作者: ilanyu 时间: Marc ...

  9. c#中的dynamic类型

    dynamic是C#4.0引入的全新类型,它允许其操作略过编译期类型检查,而在运行时期处理. dynamic类型在大多数情况下和object类似,不同点在于编译器对于dynamic类型的 数据不做进一 ...

  10. Ubuntu中将网卡名称eno16777736改回eth0

    一.前言 今天在Vmware Workstation中安装了Ubuntu Server 15.10,然后发现网卡名称出现了问题,主网卡竟然不叫“eth0”,而是叫一个奇怪的名字“eno16777736 ...