单词接龙
 单词接龙是一个与我们经常玩的成语接龙相类似的游戏,现在我们已知一组单词,且给定一个开头的字母,要求出以这个字母开头的最长的“龙”(每个单词都最多在“龙”中出现两次),在两个单词相连时,其重合部分合为一部分,例如 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. Makefile之自动变量篇

    自动变量假设您编写一个编译‘.c’文件生成‘.o’文件的规则:您怎样编写命令‘CC’,使它能够操作正确的文件名?您当然不能将文件名直接写进命令中,因为每次使用隐含规则操作的文件名都不一样. 您应该使用 ...

  2. expect学习笔记及用法

    expect学习笔记及实例详解 expect的基本用法 expect用法

  3. JSP的taglib示例

    web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="2 ...

  4. WebAPI认证与授权

    Web APi之认证(Authentication)及授权(Authorization)[一](十二) http://www.cnblogs.com/CreateMyself/p/4856133.ht ...

  5. C入门程序整体框架图

    0.1:概述, 从头开始介绍一门编程语言总是显得很困难,因为有许多的细节还没有介绍,很难让读者在大脑中形成一幅完整的图, 所以起步时以一个列程序向学折介绍大体的C,试图使大家对C有一个整体大概 影响. ...

  6. MPI 打包与解包函数 MPI_Pack(),MPI_Unpack()

    ▶ MPI 中与数据打包传输有关的几个函数 ● 函数 MPI_Pack() 与 MPI_Unpack() 的原型 MPI_METHOD MPI_Pack( _In_opt_ const void* i ...

  7. MD5 几种方法的选择

    转:http://zoroeye.iteye.com/blog/2026984?utm_source=tuicool&utm_medium=referral md5加密实现方法有很多种,也导致 ...

  8. Python运维开发基础08-文件基础

    一,文件的其他打开模式 "+"表示可以同时读写某个文件: r+,可读写文件(可读:可写:可追加) w+,写读(不常用) a+,同a(不常用 "U"表示在读取时, ...

  9. Kubernetes集群的安装部署

    此文参照https://www.cnblogs.com/zhenyuyaodidiao/p/6500830.html,并根据实操过程略作修改. 1.环境介绍及准备: 1.1 物理机操作系统 物理机操作 ...

  10. freemaker在表格中遍历数据

    Controller层如下所示: @RequestMapping(value = "/test", method = RequestMethod.GET) public Strin ...