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

Posted ymzjj

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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] == ) 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]);
30         tss[len[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] = ;
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]);
76             strcat(s[i], tmp);
77         }
78         len_ans = -1;
79         check(N);
80         if(len_ans == -1) puts("0");
81         else printf("%s
", ans);
82     }
83     return 0;
84 }
View Code

 

以上是关于ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 B Tomb Raider 二进制枚举的主要内容,如果未能解决你的问题,请参考以下文章

ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 ASaving Tang Monk II 状态搜索

ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 A.Saving Tang Monk II(优先队列广搜)

2018亚洲区预选赛北京赛站网络赛 C.Cheat

2018亚洲区预选赛北京赛站网络赛 A.Saving Tang Monk II BFS

2018ACM/ICPC 焦作网络预选赛-A Magic Mirror

2014ACM/ICPC亚洲区北京站题解