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

14 篇文章 0 订阅
5 篇文章 0 订阅

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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值