Tomb Raider HihoCoder - 1829(思维+暴力)

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.

Input

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.

Output

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

Sample Input

2
abcdefg
zaxcdkgb
5
abcdef
kedajceu
adbac
abcdef
abcdafc
2
abc
def

Sample Output

acdg
acd
0

题意:给你n个字符串,每个串为一个环,问n个字符串中最长公共子序列是什么,有相同的输出字典序最小的.
数据较小,暴力.
首先dfs处理出所有子序列,用第一个字符串的子序列做对比,如果所有字符串都出现过,更新答案.
set存储,可以去重.

#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<set>
using namespace std;
int n,len;
string ans,s;
set<string> st[11];
void dfs(int i,int cur){
	if(cur==len){
		if(ans=="")return;//更改子序列首字母
		st[i].insert(ans);
		int l=ans.length();
		for(int j=1;j<l;j++){
			st[i].insert(ans.substr(j,l-j)+ans.substr(0,j));//对子序列进行分割,substr(j,l-j)代表以j开头,长度为l-j的子序列
		}
		return;
	}
	ans=ans+s[cur];//先依次处理长度为len的子序列,依次递减.
	dfs(i,cur+1);
	ans.erase(ans.end()-1);//长度减小
	dfs(i,cur+1); 
}
int main(){
	while(~scanf("%d",&n)){
		for(int i=0;i<11;i++){
			st[i].clear();
		}
		for(int i=0;i<n;i++){
			cin>>s;
			len=s.length();
			ans="";
			dfs(i,0);.//处理每一个字符串
		}
		set<string>::iterator it;
		string a="0";
		
		for(it=st[0].begin();it!=st[0].end();it++){
			int flag=0;
			for(int i=1;i<n;i++){
				if(!st[i].count(*it)){//注意星号
					flag=1;
					break;
				}
			}
			if(!flag){
				if(a=="0"||(*it).size()>a.size())a=*it;
				if((*it).size()==a.size()&&(*it)<a)a=*it;//string可以直接比较大小
			}
		}
		cout<<a<<endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值