题目2 : 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
#include<bits/stdc++.h>
#define MAX 15
using namespace std;
typedef long long ll; string s[MAX];
int n;
string ss,ans; int find(string x){
int lenx=x.length();
for(int i=;i<n;i++){
int len=s[i].length();
int f=;
for(int k=;k<len;k++){
if(s[i][k]==x[]){
int c=;int l=-;
for(int j=k;j<len;j++){
if(s[i][j]==x[c]){
if(l==-) l=j;
c++;
if(c>=lenx&&(j-l+)<=(len/)){
f=;
break;
}
}
}
if(f==) break;
}
}
if(f==) return ;
}
return ;
}
void dfs(int en,string x,int st){
if(st>=en){
if(find(x)==){
if(x.length()>ans.length()){
ans=x;
}
else if(x.length()==ans.length()){
if(x<ans){
ans=x;
}
}
}
return;
}
for(int i=;i<=;i++){
if(i==) dfs(en,x+ss[st],st+);
else dfs(en,x,st+);
}
}
int main()
{
int t,i,j;
while(~scanf("%d",&n)){
for(i=;i<n;i++){
cin>>s[i];
s[i]+=s[i];
}
ans="{";
int len=s[].length();
for(i=;i<len;i++){
int minn=min(len-i,len/);
for(j=;j<=minn;j++){
ss=s[].substr(i,j);
dfs(j,"",);
}
}
if(ans=="{") printf("0\n");
else cout<<ans<<endl;
}
return ;
}

最新文章

  1. Spring Bean的加载
  2. 如何判定php环境是线程安全(TS)还是不安全(NTS)
  3. noConflict
  4. Python 处理数据库返回结果
  5. 类似新浪 腾讯微博字数统计 控制js(区分中英文 符号)
  6. Perl中的匹配(六)
  7. php程序备份还原mysql数据库
  8. [引]ASP.NET MVC 4 Content Map
  9. spring websocket Converters must not be empty
  10. shell惊鸿
  11. POJ 1286 Necklace of Beads(项链的珠子)
  12. ListBox第一行字体比其他行小
  13. jquery调用wcf案例
  14. cc2530学习笔记
  15. Hibernate快速入门实例
  16. BZOJ 1042: [HAOI2008]硬币购物 [容斥原理]
  17. 【安卓开发】为什么不能往Android的Application对象里存储数据
  18. K短路 (A*算法) [Usaco2008 Mar]牛跑步&amp;[Sdoi2010]魔法猪学院
  19. 斯特林公式 ——Stirling公式(取N阶乘近似值)
  20. L347

热门文章

  1. dubbo介绍及其使用案例
  2. 九度OJ 1155:鸡兔同笼 (基础题)
  3. TCP/UDP server
  4. lodash (js实用工具库)
  5. 我的Android进阶之旅------>Android如何通过自定义SeekBar来实现视频播放进度条
  6. Form表单插件
  7. WebsiteCrawler
  8. 一篇文章教你如何用R进行数据挖掘
  9. shell之起步
  10. deepin网络加速