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