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个环,上面有字母,要你删除一些字母,使最后留下了的字母都一样,要让这个剩下的长度最长

思路:先找出最短的序列,这样最省时间;

然后用容斥定理,找出所有的子序列,最大O(2^8)

判断其他每个序列有没有这个子序列

判断的方法就是,先把每个序列变长一倍,然后开始找第一个符合的字符,再在接下来的原字符长度里找出符合的字符;

如果有一个不符合,那就不可以;

把所有符合的子序列放在一起,找出最长的那个子序列;

在把这个序列变成符合输出条件的,比如bcda,要输出abcd

可以变长一倍,然后找出原长度个序列,在sort一下就可以了

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <iomanip>
#include<cmath>
#include<float.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
#define sf scanf
#define pf printf
#define scf(x) scanf("%d",&x)
#define scff(x,y) scanf("%d%d",&x,&y)
#define scfff(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define prf(x) printf("%d\n",x)
#define mm(x,b) memset((x),(b),sizeof(x))
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
typedef long long ll;
const ll mod=1e9+7;
const double eps=1e-8;
const int inf=0x3f3f3f3f;
using namespace std;
const double pi=acos(-1.0);
const int N=1e5+10;
string a[10];
int len[10];
vector<string> qq;
string ans[10];
int main()
{
int n;
while(~scf(n))
{
qq.clear();
rep(i,0,10)
ans[i]="";//初始化
int minn=11,pos;
rep(i,0,n)
{
cin>>a[i];
len[i]=a[i].length(); //记录每个的长度
if(len[i]<minn) //找出最短的序列
{
minn=len[i];
pos=i;
}
a[i]+=a[i]; //长度翻倍
}
for(int i=1;i<(1<<minn);i++)
{
string v;
for(int j=0;j<minn;j++) //找出子序列
{
if((1<<j)&i)
{
v+=a[pos][j];
}
}
int temp=0; //标记能不能找到这个序列
for(int j=0;j<n;j++) // 循环n次
{
int temp1=0; //标记这个序列能不能找到
if(j==pos) continue; //如果就是最短那个
rep(k,0,len[j]) //找一到n
{
int pos1=0;
if(a[j][k]==v[pos1]) //找符合后面的n个
{
pos1++;
for(int l=1;l<len[j];l++)
{
if(a[j][k+l]==v[pos1])
{
pos1++;
}
}
}
if(pos1==v.length()) //如果这个符合了
{
temp1=1;
break;
}
}
if(temp1==0)
{
temp=1;
break;
}
}
if(temp==0) //都能满足,放入vector
{
qq.push_back(v);
}
}
if(qq.empty()) //如果没有,输出0
{
pf("0\n");
continue;
}
int maxn=0;
string t;
rep(i,0,qq.size()) //找出最长的
{
if(qq[i].size()>maxn)
{
maxn=qq[i].size();
t=qq[i];
}
}
t+=t;
rep(i,0,maxn)
{
string aa;
rep(j,0,maxn)
{
aa+=t[i+j];
}
ans[i]=aa;
}
sort(ans,ans+maxn);
cout<<ans[0]<<endl;
}
}

Tomb Raider的更多相关文章

  1. Tomb Raider(暴力模拟)

    Tomb Raider https://hihocoder.com/problemset/problem/1829?sid=1394836 时间限制:1000ms 单点时限:1000ms 内存限制:2 ...

  2. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 B Tomb Raider 【二进制枚举】

    任意门:http://hihocoder.com/problemset/problem/1829 Tomb Raider 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 L ...

  3. ACM-ICPC2018北京网络赛 Tomb Raider(暴力)

    题目2 : Tomb Raider 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Lara Croft, the fiercely independent daughte ...

  4. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛-B:Tomb Raider(二进制枚举)

    时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Lara Croft, the fiercely independent daughter of a missing adv ...

  5. Tomb Raider HihoCoder - 1829 (二进制枚举+暴力)(The 2018 ACM-ICPC Asia Beijing First Round Online Contest)

    Lara Croft, the fiercely independent daughter of a missing adventurer, must push herself beyond her ...

  6. hihocoder1829 Tomb Raider

    思路: 暴力枚举. 实现: #include <iostream> #include <set> #include <vector> using namespace ...

  7. hihoCoder-1829 2018亚洲区预选赛北京赛站网络赛 B.Tomb Raider 暴力 字符串

    题面 题意:给你n个串,每个串都可以选择它的一个长度为n的环形子串(比如abcdf的就有abcdf,bcdfa,cdfab,dfabc,fabcd),求这个n个串的这些子串的最长公共子序列(每个串按顺 ...

  8. (转)完全用GNU/Linux工作 by 王珢

    完全用GNU/Linux工作 王珢      (看完这篇博文,非常喜欢王珢的这篇博客,也我坚定了学gnu/linux的决心,并努力去按照国外的计算机思维模式去学习编程提高自己.看完这篇文章令我热血沸腾 ...

  9. Mac使用最多的软件,整理集合

    软件资源 #[PDF移除密码]Cisdem PDFPasswordRemover 3.0.0 [TNT] #Alfred_3.1.1_737 #fwmso2016vlu2.0 #iHosts #Omn ...

随机推荐

  1. javaScript系列 [02]-javaScript对象探析

    [02]-javaScript对象探析 题记:多年前,以非常偶然的方式关注了微信公众号“面向对象”,本以为这个公众号主要以分享面向对象编程的干货为主,不料其乃实实在在的猿圈相亲平台.通过查看公开资料, ...

  2. Apache Kafka 快速入门

    概述 Apache Kafka是一个分布式发布-订阅消息系统和强大的队列,可以处理大量的数据,将消息从一个端点传递到另一个端点.Kafka适合离线和在线消息消费,Kafka消息保存在磁盘上,并在集群内 ...

  3. iOS:定制自适应大小的透明吐司弹框

    一.简单介绍 创建一个吐司消息的黑色透明弹框,可以根据消息长短自适应大小. 可以手动创建手动显示手动关闭,也可以手动创建自动显示自动关闭. 简单好用. 二.代码使用 .h文件 // // LiveHU ...

  4. tmux终端工具的简单使用

    Linux上管理和运行进程除了程序级别的守护进程之外,经常用到的有比如nohup &的方式,以及screen会话的方式,而Tmux正是一个非常优秀的终端进程管理的软件,和GNU screen类 ...

  5. NLP第9章 NLP 中用到的机器学习算法——基于统计学(文本分类和文本聚类)

  6. [Vuex] Use Namespaces in Vuex Stores using TypeScript

    Even by using modules, they still share the same namespace. So you couldn’t have the same mutation n ...

  7. 在windows命令行批量ping局域网内IP

    参考了博客园Alfred Zhao的文章<Windows平台ping测试局域网所有在用IP> 在cmd命令行运行如下命令即可: ,,) -w .%i | find "回复&quo ...

  8. 评分卡模型剖析之一(woe、IV、ROC、信息熵)

    信用评分卡模型在国外是一种成熟的预测方法,尤其在信用风险评估以及金融风险控制领域更是得到了比较广泛的使用,其原理是将模型变量WOE编码方式离散化之后运用logistic回归模型进行的一种二分类变量的广 ...

  9. GOF提出的23种设计模式是哪些 设计模式有创建形、行为形、结构形三种类别 常用的Javascript中常用设计模式的其中17种 详解设计模式六大原则

    20151218mark 延伸扩展: -设计模式在很多语言PHP.JAVA.C#.C++.JS等都有各自的使用,但原理是相同的,比如JS常用的Javascript设计模式 -详解设计模式六大原则 设计 ...

  10. Typora的使用

    Markdown是一种可以使用普通文本编辑器编写的标记语言,通过简单的标记语法,它可以使普通文本内容具有一定的格式,其目标是实现易读易写.我刚刚接触一款简单高效的Markdown编辑器–Typora, ...