ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 B Tomb Raider 【二进制枚举】

任意门:http://hihocoder.com/problemset/problem/1829

 Tomb Raider

时间限制:1000ms
单点时限:1000ms
内存限制:256MB

描述

Lara Croft, the fiercely independent daughter of a missing adventurer, must push herself beyond her limits when she discovers the island where her father disappeared. In this mysterious island, Lara finds a tomb with a very heavy door. To open the door, Lara must input the password at the stone keyboard on the door. But what is the password? After reading the research notes written in her father's notebook, Lara finds out that the key is on the statue beside the door.

The statue is wearing many arm rings on which some letters are carved. So there is a string on each ring. Because the letters are carved on a circle and the spaces between any adjacent letters are all equal, any letter can be the starting letter of the string. The longest common subsequence (let's call it "LCS") of the strings on all rings is the password. A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.

For example, there are two strings on two arm rings: s1 = "abcdefg" and s2 = "zaxcdkgb". Then "acdg" is a LCS if you consider 'a' as the starting letter of s1, and consider 'z' or 'a' as the starting letter of s2. But if you consider 'd' as the starting letter of s1 and s2, you can get "dgac" as a LCS. If there are more than one LCS, the password is the one which is the smallest in lexicographical order.

Please find the password for Lara.

输入

There are no more than 10 test cases.

In each case:

The first line is an integer n, meaning there are n (0 < n ≤ 10) arm rings.

Then n lines follow. Each line is a string on an arm ring consisting of only lowercase letters. The length of the string is no more than 8.

输出

For each case, print the password. If there is no LCS, print 0 instead.

样例输入
2
abcdefg
zaxcdkgb
5
abcdef
kedajceu
adbac
abcdef
abcdafc
2
abc
def
样例输出
acdg
acd
0

 

题意概括:

给 N 个字符环, 求这 N 个字符环的公共子序列(只考虑顺时针方向)

解题思路:

一开始是想两两配对出最长子序列再与下一个配对,用一个队列来实现字符环子序列的操作。

不过这显然是错误的做法,因为两两配对的最长子序列可能存在多种情况。

正确的打开方式:字串全部暴力一遍!!!

具体的暴力方法是二进制枚举,只暴力第一个字符环,把暴力出来的子串与剩下的字符环配对,判断是否每个字符环都存在这个子串, 如果存在保留最长的,如果相同长度保留字典序最小的(直接strcmp()就可以判断哪个子串字典序更小了)。

而处理环的方法是最常见的 double 字符串,两个相同字符串首尾相接就成环了(废话。。。)。

 

AC code:

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <cstring>
 5 using namespace std;
 6 
 7 int len[15], len_ans;
 8 char s[100+5][80+5], ans[80+5];
 9 
10 bool judge(char* ss, int k)
11 {
12     for(int f = 0; f < len[k]; f++){
13         int pss = 0;
14         for(int i = 0; i < len[k]; i++){     //逐位寻找
15             if(s[k][f+i] == ss[pss]){
16                 pss++;
17                 if(ss[pss] == '\0') return true; //所有都能找到,满足条件
18             }
19         }
20     }
21     return false;
22 }
23 
24 void check(int n)
25 {
26     char tss[20+5], css[20+5];
27     int u = 1<<len[0];                      //二进制每一位代表一个字符
28     for(int f = 0; f < len[0]; f++){        //枚举不同起点的子串
29         strncpy(tss, s[0]+f, len[0]);       // f 为起点
30         tss[len[0]] = '\0';
31         for(int k = 1; k < u; k++){         //二进制枚举当前起点的子串的子串
32             int p = 0;
33             for(int i = 0; i < len[0]; i++){
34                 if(!(k&(1<<i))) continue;
35                 css[p] = tss[i];
36                 p++;
37             }
38             css[p] = '\0';
39             bool ok = true;                 //判断其他字符环是否也存在这个子串
40             for(int i = 1; i < n; i++){
41                 if(!judge(css, i)){         //有一个不满足就匹配失败
42                     ok = false;break;
43                 }
44             }
45             if(ok){                         //如果当前子串满足条件
46                 if(len_ans == -1){          //当前子串为找到的第一个满足所有条件的子串
47                     strcpy(ans, css);
48                     len_ans = strlen(ans);
49                 }
50                 else{
51                     int lencss = strlen(css);
52                     if(lencss > len_ans){           //比较子串长度,长的优先
53                         strcpy(ans, css);
54                         len_ans = lencss;
55                     }
56                     else if(len_ans == lencss){     //长度相同,字典序小的优先
57                         if(strcmp(ans, css) > 0){
58                             strcpy(ans, css);
59                         }
60                     }
61                 }
62             }
63         }
64     }
65 }
66 
67 int main()
68 {
69     int N;
70     char tmp[20+5];
71     while(~scanf("%d", &N)){
72         for(int i = 0; i < N; i++){
73             scanf("%s", &s[i]);
74             len[i] = strlen(s[i]);
75             strcpy(tmp, s[i]);      //字符串double 处理环
76             strcat(s[i], tmp);
77         }
78         len_ans = -1;               //答案字符长度
79         check(N);                       //暴力
80         if(len_ans == -1) puts("0");    //没有满足条件的字串
81         else printf("%s\n", ans);
82     }
83     return 0;
84 }
View Code

 

转载于:https://www.cnblogs.com/ymzjj/p/9692329.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值