Best Sequence
poj1699:http://poj.org/problem?id=1699
题意:给你nge串,让你求出这些串组成的最小的串重叠部分只算一次。
题解:我的做法是DFS,因为数据范围只有10,就算是n!也只有300多万,加上剪枝,就可以过了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n;
char str[][],s[];
int ans;
bool vis[];
int getlen(char s1[],char s2[],int l1,int l2){
int ans;//表示重叠部分的长度
if(l1>=l2){
for(ans=l2;ans>=;ans--){
bool flag=true;
for(int i=;i<ans;i++){
if(s2[i]!=s1[l1-(ans-i)]){
flag=false;
break;
}
}
if(flag)return ans;
}
}
else{
for(ans=l1;ans>=;ans--){
bool flag=true;
for(int i=;i<ans;i++){
if(s2[i]!=s1[l1-(ans-i)]){
flag=false;
break;
} }
if(flag)return ans;
}
}
}
void DFS(char fa[],int ll,int dep){
if(ll>=ans)return;
if(dep==n){
ans=min(ll,ans);
return;
}
for(int i=;i<=n;i++){
if(vis[i])continue;
int tt=strlen(str[i]);
int temp=getlen(fa,str[i],ll,tt);
for(int j=ll;j<ll+(tt-temp);j++){
fa[j]=str[i][temp+(j-ll)];
}
vis[i]=;
DFS(fa,ll+tt-temp,dep+);
vis[i]=;
}
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
memset(str,,sizeof(str));
memset(s,,sizeof(s));
memset(vis,,sizeof(vis));
ans=;
for(int i=;i<=n;i++){
scanf("%s",str[i]);
}
for(int i=;i<=n;i++){
memset(s,,sizeof(s));
strcpy(s,str[i]);
vis[i]=;
DFS(s,strlen(str[i]),);
vis[i]=;
}
printf("%d\n",ans);
} }
Best Sequence的更多相关文章
- oracle SEQUENCE 创建, 修改,删除
oracle创建序列化: CREATE SEQUENCE seq_itv_collection INCREMENT BY 1 -- 每次加几个 STA ...
- Oracle数据库自动备份SQL文本:Procedure存储过程,View视图,Function函数,Trigger触发器,Sequence序列号等
功能:备份存储过程,视图,函数触发器,Sequence序列号等准备工作:--1.创建文件夹 :'E:/OracleBackUp/ProcBack';--文本存放的路径--2.执行:create or ...
- DG gap sequence修复一例
环境:Oracle 11.2.0.4 DG 故障现象: 客户在备库告警日志中发现GAP sequence提示信息: Mon Nov 21 09:53:29 2016 Media Recovery Wa ...
- Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Sequence Reconstruction 序列重建
Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- [LeetCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [LeetCode] Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- Leetcode 60. Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
随机推荐
- PHP正值表达式
一个正值表达式是有四部分组成分别是 1.定界符 //是最常用的定界符 其实除了数组字母下划线以外其他的什么都可以作为定界符 2.原子:最小的一个匹配单位 放在定界符中 在一个正值表达式中至少要有一个 ...
- Redis操作Set工具类封装,Java Redis Set命令封装
Redis操作Set工具类封装,Java Redis Set命令封装 >>>>>>>>>>>>>>>>& ...
- java strtus2 注解配置入门(一)
因为工作的原因,所以接触到一些项目,有的项目虽然看着能有跟着做,可是具体里面的框架是别人配置的,具体框架还是不是非常的了解,所以这里在看一下我学到的 一点关于struts2中注解开发的一点点. 直接代 ...
- Js 学习资料
Js 语法 http://www.php100.com/manual/jquery/ jqGrid控件 http://www.trirand.com/blog/jqgrid/jqgrid.html
- NUnit使用详解(一)
转载:http://hi.baidu.com/grayworm/item/38e246165aa7b9433a176e90 NUnit是一个单元测试框架,专门针对于.NET来写的,它是是xUnit的一 ...
- 20160501--struts2入门3
一.自定义拦截器 要自定义拦截器需要实现com.opensymphony.xwork2.interceptor.Interceptor接口: public class PermissionInterc ...
- EditText属性详解
关于EditText属性详解很详细的记录,转过来收着 1.EditText输入的文字为密码形式的设置 (1)通过.xml里设置: 把该EditText设为:android:password=" ...
- SQL Server 2012 数据库各个版本功能对比
作为这篇SQL SERVER 2008数据库各版本功能对比 的姊妹篇,就写点SQL Server 2012 各个版本的区别以及物理以及逻辑上的限制. 个部分来分http://technet.micro ...
- [转] c# 数据类型占用的字节数
http://www.cnblogs.com/laozuan/archive/2012/04/24/2467888.html
- 插入排序之python实现源码
def insert_sort(old): for i in range(1, len(old)): for j in range(i, 0, -1): if(old[j] < old[j-1] ...