Best Sequence_DFS&&KMp
Description
For example, given 'TCGG', 'GCAG', 'CCGC', 'GATC' and 'ATCG', you can slide the segments in the following way and get a sequence of length 11. It is the shortest sequence (but may be not the only one). 
Input
Output
Sample Input
1
5
TCGG
GCAG
CCGC
GATC
ATCG
Sample Output
11
【题意】找出最短的字符串长度,要求包含给出的所有字符串
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int inf=0x7777777;
int n,maxn;
int mp[][],ln[],vis[],next1[][];
char s[][];
int kmp(int x,int y)
{
int i=,j=;
while(i<ln[x])
{
if(s[x][i]==s[y][j])
{
i++;j++;
}
else
{
if(j==) i++;
else
{
j=next1[x][j];
}
}
if(j==ln[y]) return -;//能在x中找到y;
}
return j;
}
void get_next(int k)
{
int j=-;
next1[k][]=-;
for(int i=;i<ln[k];i++)
{
if(j==-||s[k][i]==s[k][j]) next1[k][++i]=++j;
else j=next1[k][j];
}
}
void dfs(int len,int cnt,int k)
{
if(cnt==n)//已经是第n个字符串了,获取最小的长度
{
if(len<maxn) maxn=len;
return;
}
for(int i=;i<=n;i++)
{
if(vis[i]==)
{
vis[i]=;
if(mp[k][i]==-)//如果字符串k和i包含关系
{
dfs(len,cnt+,k);//最后一个参数仍然是k
}
else dfs(len+ln[i]-mp[k][i],cnt+,i);//如果不包含,两个串的长度相加减去相同部分长度
vis[i]=;
}
}
return;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%s",s[i]);
ln[i]=strlen(s[i]);
get_next(i);//求next数组
}
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
mp[i][j]=kmp(i,j);//求两个字符串的匹配度
}
}
maxn=inf;
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++)
{
vis[i]=;
dfs(ln[i],,i);
vis[i]=;
}
printf("%d\n",maxn);
}
return ;
}
Best Sequence_DFS&&KMp的更多相关文章
- KMP算法求解
// KMP.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> using namespac ...
- 简单有效的kmp算法
以前看过kmp算法,当时接触后总感觉好深奥啊,抱着数据结构的数啃了一中午,最终才大致看懂,后来提起kmp也只剩下“奥,它是做模式匹配的”这点干货.最近有空,翻出来算法导论看看,原来就是这么简单(先不说 ...
- KMP算法
KMP算法是字符串模式匹配当中最经典的算法,原来大二学数据结构的有讲,但是当时只是记住了原理,但不知道代码实现,今天终于是完成了KMP的代码实现.原理KMP的原理其实很简单,给定一个字符串和一个模式串 ...
- 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)
前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...
- [KMP]【学习笔记】
Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 36916 Accepted: 14904 Descript ...
- KMP算法实现
链接:http://blog.csdn.net/joylnwang/article/details/6778316 KMP算法是一种很经典的字符串匹配算法,链接中的讲解已经是很明确得了,自己按照其讲解 ...
- KMP专题
1.[HDU 3336]Count the string(KMP+dp) 题意:求给定字符串含前缀的数量,如输入字符串abab,前缀是a.ab.aba.abab,在原字符串中出现的次数分别是2.2.1 ...
- KMP学习之旅
说起kmp就要从字符串的匹配说起,下面我们谈谈字符串的匹配 给定一个原字符串:bababababababababb,再给定一个模式串:bababb,求模式串是否在源字符串中出现 最简单的方法就是遍历源 ...
- KMP模板
参考:http://www.cnblogs.com/c-cloud/p/3224788.html #include<stdio.h> #include<string.h> vo ...
随机推荐
- RestSharp .net 轻量级rest客户端
RestSharp Simple REST and HTTP API Client for .NET 官网:http://restsharp.org/ GiHub: https://github.co ...
- poj1258 Agri-Net (prim+heap)
题目链接:poj1258 Agri-Net 这题我上个月做过,是个大水题,今天看见有人用prim+heap做的,就学习了下. #include<cstdio> #include<cs ...
- org.hibernate.LazyInitializationException: could not initialize proxy - no Session
原因:在延迟加载的状态下,使用某个属性时,但session已经关闭. 解决方法: 1.把load改成get,直接加载所有属性. 2.获取对象进行一次判断,如果对象没有初始化,就进行一次初始化. if ...
- Axis2 webservice入门--开发环境搭建,概念理解
关于webservice的概念,网上有各种解释,但是不太好懂. 可以这样理解:1.一个webservice就是一个“功能”,只是这个功能是别人写好的,被放在别人的网站上. ...
- heartbeat安装
wget ftp://mirror.switch.ch/pool/1/mirror/scientificlinux/6rolling/i386/os/Packages/epel-release-6-5 ...
- C#启动一个外部程序(1)-WinExec
C#启动一个外部程序(1)-WinExec 调用Win32 API.1. using System.Runtime.InteropServices; 2. // //#define SW ...
- PHP安装pthreads多线程扩展教程[windows篇]
from:http://blog.csdn.net/aoyoo111/article/details/19020161 一.判断PHP是ts还是nts版 通过phpinfo(); 查看其中的 Thre ...
- display:flex
元素在x方向走,元素y不一样[高度].可以用对齐.align-items. align-self 自身调节元素在x方向走,元素在x方向距离.justify-content . 元素在x方向走,x方 ...
- 开通了cnblogs
受够了百度空间,换个地方,或许会更好. 以后有机会会将百度空间你的文章搬过来的.
- Hibernate中的组合映射
1.实体bean设计 car: public class Car { private int id; private String name; private Wheel wheel; set... ...