ACM/ICPC 2018亚洲区预选赛北京网络赛 B.Tomb Raider(暴力)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Cymbals/article/details/82879022

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.

求n个循环串的lcs。
看起来像个神级问题,但是由于n只有10,字符串长度只有8,所以暴力上,枚举出每个循环串的所有子序列,然后合并集合,取字典序最小。

至于循环串,用经典的把s变成ss就能解决,然后dfs串枚举,注意扩大成ss之后枚举区间应该在起始位置到起始位置+s的长度。

ac代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int n, len, idx;
string s;
set<string> st[10];

void dfs(string m, int fa, int start) {
	st[idx].insert(m);
	for(int i = start + 1; i < s.length() && i < fa + len; i++) {
		dfs(m + s[i], fa, i);
	}
}

int main() {
	ios::sync_with_stdio(0);
	while(cin >> n) {
		for(idx = 0; idx < n; idx++) {
			cin >> s;
			len = s.length();
			s = s + s;
			for(int i = 0; i < len; i++) {
				dfs(s.substr(i, 1), i, i);
			}
		}
		string ans = "0";
		for(auto i : st[0]) {
			bool flag = 0;
			for(int j = 1; j < n; j++) {
				if(!st[j].count(i)) {
					flag = 1;
					break;
				}
			}
			if(!flag) {
				if(ans[0] == '0'){
					ans = i;
					continue;
				}
				if(i.length() > ans.length()) {
					ans = i;
				} else if(i.length() == ans.length()) {
					ans = min(ans, i);
				}
			}
		}
		for(int i = 0; i < n; i++) {
			st[i].clear();
		}
		cout << ans << endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Cymbals/article/details/82879022