时间限制: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

题意

有n个字符串,每个字符串首尾相连,求这n个字符串的最长公共子序列并输出

思路

将每个首尾相连字符串从0~len-1的每个位置形成的字符串都进行二进制枚举,将所有情况的子序列都用map标记并统计出现的次数,然后枚举map里的元素,将出现n次的子序列存进vector,对vector里的元素进行排序。排序的规则:如果两个子序列长度不同,返回长度较长的子序列,如果相同,返回字典序小的子序列

AC代码

#include<bits/stdc++.h>
using namespace std;
#define line cout<<"------------"<<endl
const int N = 33; map<string, int>mp,vis;
map<int, string>ms;
vector<string>ve;
int n;
string s,u, str;
bool cmp(string a, string b)
{
if(a.length() != b.length())
return a.length() > b.length();
else
return a < b;
}
// 求出从pos位置开始的字符串
void change(int pos)
{
str.clear();
string temp = s.substr(pos);
str += temp;
temp = s.substr(0,pos);
str += temp;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
mp.clear();
ve.clear();
int nn=n;
while(nn--)
{
vis.clear();
cin>>s;
int len=s.length();
for(int k=0; k<len; k++)
{
change(k);
// 对字符串进行二进制枚举,每个子序列出现的次数用map标记
for(int i=1;i < (1<<len); i++)
{
for(int j=0; j<len; j++)
{
if(i >> j & 1)
u += str[j];
}
if(vis[u]==0)
{
mp[u]++;
vis[u]=1;
}
u.clear();
}
}
}
int flag=0;
for(auto i: mp)
{
// 找出所有出现次数为n的子序列,存进vector
if(i.second == n)
{
ve.push_back(i.first);
flag = 1;
}
}
// 对vector进行排序
sort(ve.begin(),ve.end(),cmp);
if(flag==0)
cout<<0<<endl;
else cout << ve[0] << endl;
}
return 0;
}

ACM/ICPC 2018亚洲区预选赛北京赛站网络赛-B:Tomb Raider(二进制枚举)的更多相关文章

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

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

  2. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛D-80 Days--------树状数组

    题意就是说1-N个城市为一个环,最开始你手里有C块钱,问从1->N这些城市中,选择任意一个,然后按照顺序绕环一圈,进入每个城市会有a[i]元钱,出来每个城市会有b[i]个城市,问是否能保证经过每 ...

  3. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 80 Days(尺取)题解

    题意:n个城市,初始能量c,进入i城市获得a[i]能量,可能负数,去i+1个城市失去b[i]能量,问你能不能完整走一圈. 思路:也就是走的路上能量不能小于0,尺取维护l,r指针,l代表出发点,r代表当 ...

  4. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛

    题意:到一个城市得钱,离开要花钱.开始时有现金.城市是环形的,问从哪个开始,能在途中任意时刻金钱>=0; 一个开始指针i,一个结尾指针j.指示一个区间.如果符合条件++j,并将收益加入sum中( ...

  5. hihoCoder #1831 : 80 Days-RMQ (ACM/ICPC 2018亚洲区预选赛北京赛站网络赛)

    水道题目,比赛时线段树写挫了,忘了RMQ这个东西了(捞) #1831 : 80 Days 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 80 Days is an int ...

  6. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 A、Saving Tang Monk II 【状态搜索】

    任意门:http://hihocoder.com/problemset/problem/1828 Saving Tang Monk II 时间限制:1000ms 单点时限:1000ms 内存限制:25 ...

  7. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 A.Saving Tang Monk II(优先队列广搜)

    #include<bits/stdc++.h> using namespace std; ; ; char G[maxN][maxN]; ]; int n, m, sx, sy, ex, ...

  8. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 D 80 Days (线段树查询最小值)

    题目4 : 80 Days 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 80 Days is an interesting game based on Jules Ve ...

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

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

随机推荐

  1. China’s movie heroes 《红海行动》展现中国英雄本色

    Recent years have seen a trend for big military movies. Whether it was last year’s British hit Dunki ...

  2. 从概率图模型pgm到rbm

    有向图模型:directed acyclic graph  DAG  贝叶斯网络 对称的,无向图, UGM: undirected graphic model  UGM, 更有名的名称是MRF,mar ...

  3. git Please move or remove them before you can merge.

    git clean -d -fx "" 其中  x -----删除忽略文件已经对git来说不识别的文件 d -----删除未被添加到git的路径中的文件 f -----强制运行

  4. angular 我看过的技术书籍

    13年我在悠唐网络做前端开发时,当时仿豌豆荚一个sdk 发布应用界面的时候,看到代码用到奇怪的ng-,当时查了下是用angular,从那时开始慢慢接触angular,之后进入逸橙官网组使用angula ...

  5. day34 线程池 协程

    今日内容: 1. 线程的其他方法 2.线程队列(重点) 3.线程池(重点) 4.协程 1.线程的其他方法 语法: Threading.current_thread() # 当前正在运行的线程对象的一个 ...

  6. pytorch加载和保存模型

    在模型完成训练后,我们需要将训练好的模型保存为一个文件供测试使用,或者因为一些原因我们需要继续之前的状态训练之前保存的模型,那么如何在PyTorch中保存和恢复模型呢? 方法一(推荐): 第一种方法也 ...

  7. Cracking The Coding Interview 5.6

    //Write a program to swap odd and even bits in an integer with as few instructions as possible (e.g. ...

  8. form 表单模板

    <div class="modal-dialog modal-lg"> //大布局modal-lg <div class="modal-content& ...

  9. 7 Serial Configuration 理解(二)

    *Serial Configuration Mode 串行配置模式分为:Master Serial 和 Slave Serial (如下图)两类: 两者的区别在与CCLK的输入输出方向:主动模式下为输 ...

  10. Java作业五

    1.编程生成10个1~100之间的随机数,并统计每个数出现的概率. 这个博文里面又random的详细解释:https://www.cnblogs.com/ningvsban/p/3590722.htm ...