Best Sequence

poj1699:http://poj.org/problem?id=1699

题意:给你nge串,让你求出这些串组成的最小的串重叠部分只算一次。

题解:我的做法是DFS,因为数据范围只有10,就算是n!也只有300多万,加上剪枝,就可以过了。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 int n;
 7 char str[100][100],s[10000];
 8 int ans;
 9 bool vis[100];
10 int getlen(char s1[],char s2[],int l1,int l2){
11       int ans;//表示重叠部分的长度
12       if(l1>=l2){
13          for(ans=l2;ans>=0;ans--){
14              bool flag=true;
15            for(int i=0;i<ans;i++){
16                 if(s2[i]!=s1[l1-(ans-i)]){
17                     flag=false;
18                     break;
19                 }
20           }
21           if(flag)return ans;
22       }
23     }
24     else{
25      for(ans=l1;ans>=0;ans--){
26              bool flag=true;
27            for(int i=0;i<ans;i++){
28                 if(s2[i]!=s1[l1-(ans-i)]){
29                     flag=false;
30                     break;
31                 }
32
33           }
34           if(flag)return ans;
35       }
36     }
37 }
38 void DFS(char fa[],int ll,int dep){
39       if(ll>=ans)return;
40       if(dep==n){
41         ans=min(ll,ans);
42         return;
43       }
44       for(int i=1;i<=n;i++){
45           if(vis[i])continue;
46           int tt=strlen(str[i]);
47           int temp=getlen(fa,str[i],ll,tt);
48           for(int j=ll;j<ll+(tt-temp);j++){
49             fa[j]=str[i][temp+(j-ll)];
50           }
51           vis[i]=1;
52           DFS(fa,ll+tt-temp,dep+1);
53           vis[i]=0;
54       }
55 }
56 int main(){
57   int T;
58   scanf("%d",&T);
59   while(T--){
60     scanf("%d",&n);
61     memset(str,0,sizeof(str));
62     memset(s,0,sizeof(s));
63     memset(vis,0,sizeof(vis));
64     ans=10000000;
65     for(int i=1;i<=n;i++){
66         scanf("%s",str[i]);
67     }
68     for(int i=1;i<=n;i++){
69       memset(s,0,sizeof(s));
70        strcpy(s,str[i]);
71         vis[i]=1;
72         DFS(s,strlen(str[i]),1);
73         vis[i]=0;
74     }
75     printf("%d\n",ans);
76   }
77
78 }

时间: 2024-08-10 22:12:31

Best Sequence的相关文章

LeetCode OJ - Longest Consecutive Sequence

这道题中要求时间复杂度为O(n),首先我们可以知道的是,如果先对数组排序再计算其最长连续序列的时间复杂度是O(nlogn),所以不能用排序的方法.我一开始想是不是应该用动态规划来解,发现其并不符合动态规划的特征.最后采用类似于LRU_Cache中出现的数据结构(集快速查询和顺序遍历两大优点于一身)来解决问题.具体来说其数据结构是HashMap<Integer,LNode>,key是数组中的元素,所有连续的元素可以通过LNode的next指针相连起来. 总体思路是,顺序遍历输入的数组元素,对每个

1005 Number Sequence

Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A, B, and n, you are to calculate the value of f(n). Input The input consists of multiple test cases. Each test case co

HDU 5783 Divide the Sequence(数列划分)

HDU 5783 Divide the Sequence(数列划分) Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)   Problem Description - 题目描述 Alice has a sequence A, She wants to split A into as much as possible continuous subsequences, satisfy

HDU 3397 Sequence operation(线段树)

HDU 3397 Sequence operation 题目链接 题意:给定一个01序列,有5种操作 0 a b [a.b]区间置为0 1 a b [a,b]区间置为1 2 a b [a,b]区间0变成1,1变成0 3 a b 查询[a,b]区间1的个数 4 a b 查询[a,b]区间连续1最长的长度 思路:线段树线段合并.须要两个延迟标记一个置为01,一个翻转,然后因为4操作,须要记录左边最长0.1.右边最长0.1,区间最长0.1,然后区间合并去搞就可以 代码: #include <cstdi

HDU 3998 Sequence (最长递增子序列+最大流SAP,拆点法)经典

Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1666    Accepted Submission(s): 614 Problem Description There is a sequence X (i.e. x[1], x[2], ..., x[n]). We define increasing subsequ

HDOJ 5147 Sequence II 树状数组

树状数组: 维护每一个数前面比它小的数的个数,和这个数后面比他大的数的个数 再枚举每个位置组合一下 Sequence II Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 121    Accepted Submission(s): 58 Problem Description Long long ago, there is a seq

Acdream 1427 Nice Sequence

Nice Sequence Time Limit: 12000/6000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) Problem Description Let us consider the sequence a1, a2,..., an of non-negative integer numbers. Denote as ci,j the number of occurrences of the number i a

hdu5014 Number Sequence(异或运算)

题目链接: huangjing 题意: 这个题目的意思是给出0~n的排列,然后找出与这个序列的配对使(a0 ⊕ b0) + (a1 ⊕ b1) +·+ (an ⊕ bn)最大.. 思路: 从大到小遍历每个数,然后找到与这个数二进制位数互补的数,那么他们的抑或值必定是pow(2,n)-1,,肯定是最大的.... 题目: Number Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav

HDU 5014 Number Sequence(2014 ACM/ICPC Asia Regional Xi&#39;an Online) 题解

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5014 Number Sequence Problem Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● ai ∈ [0,n] ● ai ≠ aj( i ≠ j ) For sequence a and sequ

HDU 6065 RXD, tree and sequence (LCA DP)

RXD, tree and sequence Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 234    Accepted Submission(s): 82 Problem Description RXD has a rooted tree T with size n, the root ID is 1, with the dep