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 ...
随机推荐
- XCode模拟器屏幕显示内容非常慢
今天遇到了一个问题,就是XCode模拟器屏幕显示的非常慢,但是点击一下模拟器,内容就会马上显示出来,最后查找出问题是自己不注意把刷新UI界面的代码放到了异步线程中去执行了,刷新UI界面的代码放到主线程 ...
- 页面常见效果js实现
2015.12.2 页面常见效果js实现 [有没有觉得很坑,[笑哭,邮箱写上]] 复习: Js内置对象: 1.浏览器对象 window document history location event ...
- H5 canvas 小demo之小球的随机运动
1:结构之html----balls.html <!DOCTYPE html> <html> <head lang="en"> <meta ...
- C# select的联动效果
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head&g ...
- 通过js实时检测文本框内容
思路 1,在获取文本框焦点后,启动定时器,每隔100毫秒来查看文本内容的改变 2,在文本框失去焦点后,清除定时器 下面是一个简单的例子 <!DOCTYPE html> <html&g ...
- SqlServer优化博客网址
CareySon Sql Server MVP : http://www.cnblogs.com/CareySon/
- 通过javascript库JQuery实现页面跳转功能代码
通过javascript库JQuery实现页面跳转功能代码的四段代码实例如下. 实例1: 1 2 3 4 $(function(){ var pn = $("#gotopagenum&quo ...
- Spring的多配置文件加载
如果配置文件存在多个的情况下,加载配置文件的方式是:1--可以指定总的配置文件去包含子的配置文件,然后只加载总的配置文件即可在总配置文件applicationContext.xml 中引入子文件 &l ...
- swift-01-利用元组判断字符串出现次数
//问题的提出:有一个字符串 array = ["1","2","4","4","2"," ...
- yum安装lamp环境
装了好些次lamp环境了,都没好好总结下,现在总结下 ^ ^ 1.替换163的yum源 1.检查系统版本 cat /etc/redhat-releas (我的版本是CentOS release 6 ...