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

题目链接:hihoCoder #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个字符串,首尾相连,求最长公共子序列,若答案多个,输出字典序最小的

思路:暴力把每个字符串的每个子序列放入map(不可重复放入),出现n次的就是公共的子序列,符合条件的公共子序列都放入数组,最后排序,得到最长中字典序最小的那个 

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<map>
#include<vector>
#include<string>
using namespace std;

string s;
string newstr(int pos){
	string news1(s,pos);
	string news2(s,0,pos);
	string news=news1+news2;
	return news;
}
bool cmp(string a,string b){
	if(a.length()!=b.length())return a.length()>b.length();
	return a<b;
}
int main(){
	int n;
	while(~scanf("%d",&n)){
		map<string,int>mp,vis;
		map<string,int>::iterator it;
		for(int i=1;i<=n;i++){
			cin>>s;
			vis.clear();
			int len=s.length();
			for(int j=0;j<len;j++){//遍历任意位置开头的字符串 
				string news=newstr(j);
				for(int k=1;k<(1<<len);k++){//遍历一个字符串的每个子序列 
					string str;
					for(int p=0;p<len;p++){
						if(k>>p&1){
							str+=news[p];
						}
					}
					if(vis[str]==0){//同一个字符串,在遍历中会有重复子序列出现,要排除 
						vis[str]=1;
						mp[str]++;
					}
				}
			}
		}
		int flag=0;
		vector<string>ans;
		for(it=mp.begin();it!=mp.end();it++){
			if(it->second==n){//出现n次,符合条件,放入ans 
				ans.push_back(it->first);
				flag=1;
			}
		}
		sort(ans.begin(),ans.end(),cmp);
		if(!flag)cout<<0<<endl;
		else cout<<ans[0]<<endl;
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值