Description
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个字符串的最长公共子序列。
0 < n ≤ 10,字符串长度不超过8,若有多个答案则输出字典序最小的。
核心思想:
最外层for循环,枚举最长公共子序列的长度。
对于每个枚举出的公共子序列长度len:
- 用map容器mp[i]维护前i个字符串长度为len的公共子序列。
遍历n个字符串。
细节见代码。
代码如下:
#include<cstdio>
#include<iostream>
#include<map>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
map<string,int>mp[12];
string s[12],t,ans,tans="zzzzzzzzzzzzz";
//len是枚举的公共子序列长度
//z是当前研究的字符串
//ez=n-1,el=s[z].size
int len,z,ez,el;
//dfs用来从el个字符中选出len个
//x是搜索到的位置,k是还需要选几个,p是已经选的字符
void dfs(int x,int k,string p)
{
//余下的不足k个,剪枝
if(k>el-x)
return;
//选够了,dfs出口
if(k==0)
{
//p加倍铺开
p+=p;
//枚举起点
for(int i=0;i<len;i++)
{
int en=i+len;
//t是s[z]的一个长度为len的子序列
t.clear();
for(int j=i;j<en;j++)
t+=p[j];
//如果z=n-1
if(z==ez)
{
//如果t又是前n-1个字符串的公共子序列
if(mp[z-1][t])
ans=min(ans,t);
}
//判断t是不是前z个字符串的公共子序列
else if(!z||mp[z-1][t])
mp[z][t]=1;
}
return;
}
dfs(x+1,k,p);
dfs(x+1,k-1,p+s[z][x]);
return;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
ans=tans;
//n=1特判
if(n==1)
{
cin>>s[0];
len=s[0].size();
//将s加倍铺开
s[0]+=s[0];
//枚举起点
for(int i=0;i<len;i++)
{
t.clear();
int en=i+len;
for(int j=i;j<en;j++)
t+=s[0][j];
//取字典序最小
ans=min(ans,t);
}
cout<<ans<<endl;
continue;
}
len=20;
ez=n-1;
//输入
for(int i=0;i<n;i++)
{
cin>>s[i];
int tl=s[i].size();
//len最大是所有字符串长度的最小值
len=min(len,tl);
}
//枚举最长公共子序列长度
for(;len>0;len--)
{
//初始化mp[i]
for(int i=0;i<12;i++)
mp[i].clear();
//挨着对s[i]进行深搜
for(z=0;z<n;z++)
{
el=s[z].size();
dfs(0,len,"");
}
//出现答案
if(ans<tans)
{
cout<<ans<<endl;
break;
}
}
//无公共子序列
if(ans==tans)
printf("0\n");
}
return 0;
}

在神秘岛上,拉拉必须找到父亲失踪的秘密。通过解读父亲留下的线索,她发现了一个隐藏的古墓,门上有一个由雕像上的字母环构成的密码锁。任务是找出这些字母环上的最长公共子序列,作为打开墓门的密码。
219

被折叠的 条评论
为什么被折叠?



