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

    https://en.wikipedia.org/wiki/List_of_LTE_networks

  2. 浅析 MySQL Replication(转)

    目前很多公司中的生产环境中都使用了MySQL Replication ,也叫 MySQL 复制,搭建配置方便等很多特性让 MySQL Replication 的应用很广泛,我们曾经使用过一主拖20多个 ...

  3. java web Servlet开发(一)

    一.Servlet简介 Servlet是sun公司提供的一门用于开发动态web资源的技术. Sun公司在其API中提供了一个servlet接口,用户若想用发一个动态web资源(即开发一个Java程序向 ...

  4. phalcon安装笔记

    参考官网 git clone --depth= git://github.com/phalcon/cphalcon.git cd cphalcon/build sudo ./install 添加配置文 ...

  5. pyinstaller的使用方法 by 王大龙

    ---------------------------------------------------------------------------------------------------- ...

  6. Fri Oct 31 18:00:00 UTC+0800 2008转换为yyyy-mm-dd

    这个其实网上有很多例子,都是直接用js在前端做了时间处理,我的处理也一样,想要变成2008-3-31,就用下面的js直接可以处理 function Todate(num) { //Fri Oct 31 ...

  7. MySQL my.cnf参数配置优化详解

    [b]PS:本配置文件针对Dell R710,双至强E5620.16G内存的硬件配置.CentOS -100-300w的站点,主要使用InnoDB存储引擎.其他应用环境请根据实际情况来设置优化.[/b ...

  8. 如何在Solr中实现多core查询

    基于solr或者elasticsearch提供的多核,多索引,多shard等查询能力,一般都是由lucene提供的多索引查询的功能演化而来的,这个功能在单机版的lucene里面确实没有发挥多大的威力, ...

  9. selenium+python自动化83-pip安装selenium报Read time out HTTPSConnectionPool(host='pypi.python.org' port443)

    遇到问题 1.有些小伙伴在用pip安装selenium时候报 Read time out HTTPSConnectionPool(host='pypi.python.org' port443) 2.估 ...

  10. php自动生成mysql的触发代码。

    如果公司里有上百个表要做触发器,如果手动写代码的话.很累,所以今天写了一个小程序, <?php $dbname = 'test';//数据库 $tab1 = 'user'; //执行的表 $ta ...