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 ...
随机推荐
- 学习tcl的资源
在这里介绍一些学习tcl的资源,有问题的时候可以尝试从这些资源中获取帮助. 网站: http://www.tcl.tk 官方站点 http://www.scriptics ...
- Magento 中的多个类别的筛选产品集合
通过在 Magento 中的多个类别的筛选产品集合. 按只 1 类别筛选 Magento 提供筛选器,可以使用直接从该集合的类型: $_category = Mage::getModel('catal ...
- C# 类是怎么执行的?
C# 类是怎么执行的? public class Person{ static person(){} //不写,默认也有个空的 public person(){}//不写,默认也有个空的 public ...
- js一些算法实现
1.约瑟夫环实现 //附有调试 function joseph(n,p){ var arr=[]; for(var i=0;i<n;i++){ arr.push(i); } debugger; ...
- 向SQL2008R2导入Acess、excel数据
一:导入Access数据 1.在sql2008查询分析 器中输入如下查询语句能查出access中的数据 SELECT * FROM OpenDataSource( 'Microsoft.Jet.OLE ...
- ios NSAssert趣谈
Apple 官网介绍 NSAssert 的定义如下: #define NSAssert(condition, desc, ...) \ do { \ __PRAGMA_PUSH_NO_EXTRA_AR ...
- asp.net 用jquery判断fileupload上传文件的大小和类型和名字
<script language="javascript" type="text/javascript"> //检查上传文件大小和获取文件名 fun ...
- O-C相关-07-@property关键字简介与使用
基本概念:在O-C中,创建完类之后还需要给一个类添加属性和方法,之前说过的set和get方法比较繁琐,因此引入了@property 这个编译器指令.@property 是一个编译器指令.所谓的编译器指 ...
- c语言中结构体的定义、初始化及内存分配
#include <stdio.h> struct person { char *name; int age; }; int main() { //结构体可以定义在函数内,也可以定义到函数 ...
- C#微信开发之旅--基本信息的回复
上一篇说到配置和验证<C#微信开发之旅--准备阶段> 下面来实现一下简单的信息回复. 也就是接收XML,返回XML 可以去看下微信开发文档的说明:http://mp.weixin.qq.c ...