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
描述
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] == ‘