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的更多相关文章

  1. oracle SEQUENCE 创建, 修改,删除

    oracle创建序列化: CREATE SEQUENCE seq_itv_collection            INCREMENT BY 1  -- 每次加几个              STA ...

  2. Oracle数据库自动备份SQL文本:Procedure存储过程,View视图,Function函数,Trigger触发器,Sequence序列号等

    功能:备份存储过程,视图,函数触发器,Sequence序列号等准备工作:--1.创建文件夹 :'E:/OracleBackUp/ProcBack';--文本存放的路径--2.执行:create or ...

  3. DG gap sequence修复一例

    环境:Oracle 11.2.0.4 DG 故障现象: 客户在备库告警日志中发现GAP sequence提示信息: Mon Nov 21 09:53:29 2016 Media Recovery Wa ...

  4. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  5. [LeetCode] Sequence Reconstruction 序列重建

    Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...

  6. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  7. [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 ...

  8. [LeetCode] Longest Consecutive Sequence 求最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  9. [LeetCode] Permutation Sequence 序列排序

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  10. 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 ...

随机推荐

  1. JVM笔记3:Java垃圾收集算法与垃圾收集器

    当前商业虚拟机的垃圾收集都采用"分代收集"算法,即根据对象生命周期的不同,将内存划分几块,一般为新生代和老年代,不同的代根据其特点使用最合适的垃圾收集算法 一,标记-清除算法: 该 ...

  2. Ajax返回数据类型

    MVC中,如果从controller返回的不是一个html,而是一个文本,使用AJAX中如何获取? 后台代码: public ActionResult UploadPicture() { return ...

  3. HTML解析引擎:Jumony 开源项目

    Jumony Core首先提供了一个近乎完美的HTML解析引擎,其解析结果无限逼近浏览器的解析结果.不论是无结束标签的元素,可选结束标签的元素,或是标记属性,或是CSS选择器和样式,一切合法的,不合法 ...

  4. 关于使用navigationController,前后2个视图控制器navigationBar隐藏属性不同,导致右滑手势失效问题的解决办法

    ###1.问题描述:如A是navigationController的rootViewController,在这个页面navigationBar是显示的(隐藏属性为NO),它push圧栈过来B视图控制器 ...

  5. iOS svn版本回退 cornerstone

    http://blog.csdn.net/x32sky/article/details/46866899   IOS开发中,SVN如何恢复到某一个版本(以Cornerstone为例) Cornerst ...

  6. JS模版引擎[20行代码实现模版引擎读后感]

    曾经阅读过<只有20行JAVASCRIPT代码, 手把手教你写一个页面模版引擎>这篇文章, 对其中实现模版的想法实在膜拜, 于是有了这篇读后感, 谈谈自己对模版引擎的理解, 以及用自己的语 ...

  7. Linux下的另一个词典GoldenDict

    以前一直在用statdict,突然发现了一个好用的东西Goldendict. 转载丁香园上一篇文章:http://www.dxy.cn/bbs/topic/20455142 Goldendict 话说 ...

  8. 关于C++几个容易混淆的概念总结

    1.函数指针和指针函数 函数指针说的就是一个指针,但这个指针指向的函数,不是普通的基本数据类型或者类对象.函数指针的定义: int (*func)(int a, int b); // 声明函数指针 指 ...

  9. (hdu)1950 Bridging signals(最长上升子序列)

    Problem Description 'Oh no, they've done it again', cries the chief designer at the Waferland chip f ...

  10. Android清空画布

    public void clear() { Paint paint = new Paint(); paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR ...