Tomb Raider

https://hihocoder.com/problemset/problem/1829?sid=1394836

时间限制: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 求所有字符串的公共的LCS,因为数据范围很小,所以可以把第一个字符串的所有子序列求出来,和剩余的一个一个比较
注意,n是大于0的,所以有可能只有一个字符串。因为被看清这点,多调了半小时。。。
 #include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstdio>
using namespace std; string s[];
string ans[];
int main(){ int n;
string str;
while(cin>>n){
for(int i=;i<=n;i++){
cin>>s[i];
}
int coun=;
int len=s[].length();
int num=<<len;
for(int i=;i<num;i++){
int j=i;
str="";
for(int k=;k<len;k++){
if((j>>k)&){
str+=s[][k];
}
}
int co=;
for(int k=;k<=n;k++){
for(int h=;h<s[k].length();h++){
int len1=str.length();
int len2=s[k].length();
int l1=,l2=;
while(l1<len1&&l2<len2){
if(str[l1]==s[k][l2]){
l1++;
l2++;
}
else{
l2++;
}
}
if(l1==len1){
co++;
break;
}
char hh=s[k][];
s[k]=s[k].substr();
s[k]+=hh;
}
if(co==n){
ans[coun++]=str;
}
}
}
int prelen=;
if(coun==){
cout<<<<endl;
}
else{
for(int i=;i<coun;i++){
if(prelen<ans[i].length()){
prelen=ans[i].length();
}
}
string aa; for(int i=;i<coun;i++){
if(prelen==ans[i].length()){
aa=ans[i];
break;
}
}
string pre="zzzzzzzzzzzz";
for(int i=;i<aa.length();i++){
char tmp=aa[];
aa=aa.substr();
aa+=tmp;
if(pre>aa){
pre=aa;
}
}
cout<<pre<<endl;
} } }
 

最新文章

  1. [已解决] 日常开发中禁用Tomcat自动重启
  2. 鼠标模拟点击a标签
  3. 《大话移动APP测试:Android与iOS应用测试指南》
  4. iOS - OC NSNumber 数字
  5. centos 7下配置mysql+php(ThinkPHP)+nginx
  6. UVa11248 Frequency Hopping(最大流+最小割)
  7. Asp服务器控件(HyperLink、Button) 绑定后台参数 DataBinder.Eval
  8. php----浅谈一下empty isset is_null的用处
  9. HDU 2527
  10. 基于.NET Socket Tcp的发布-订阅框架
  11. SpringMVC轻松学习-其他常用(四)
  12. Docker - docker machine
  13. maven打包 tomcat运行pom配置 或 打成jar包
  14. 在vue中优雅地实现简单页面逆传值
  15. PAT1092:To Buy or Not to Buy
  16. 一、Activity的生命周期和启动模式
  17. webpack4与babel配合使es6代码可运行于低版本浏览器
  18. BZOJ.2054.疯狂的馒头(并查集)
  19. Hadoop 设置任务执行的队列以及优先级
  20. P5016 龙虎斗 题解

热门文章

  1. 杂项:ORM
  2. sersync基于rsync+inotify实现数据实时同步
  3. [UE4]多播代理
  4. Parallel I/O and Columnar Storage
  5. Bogart gGrid.vb
  6. 使用SharedPreferences存储数据
  7. Java的Synchronized
  8. 6、SpringMVC源码分析(1):分析DispatcherServlet.doDispatch方法,了解总体流程
  9. springboot - SqlSessionFactoryBean falls in circular dependencies by Spring Boot&#39;s DataSourceInitializer
  10. Virus