时间限制: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. Xmind settings lower

    Xmind settings lower   1● setting 2● options 3● fast short keys     快捷键(Windows) 快捷键(Mac) 描述 Ctrl+N ...

  2. H5离线缓存技术Application Cache

    H5离线缓存技术Application Cache 1.离线缓存技术:是浏览器本身的一种机制 HTML5引入Application Cache(应用程序缓存)技术,离线存储可以将站点的一些文件存储在本 ...

  3. Win10系列:JavaScript页内导航

    页内导航是在一个页面内根据需要加载其他页面的内容,在开发基于JavaScript的Windows应用商店应用时,可以使用WinJS.Navigation.navigate函数传递要加载的页面地址并使用 ...

  4. 网页设置下载apk

     APK文件其实是zip格式,但后缀名被修改为apk,通过UnZip解压后,可以看到Dex文件,Dex是Dalvik VM executes的全称,即Android Dalvik执行程序,并非Ja ...

  5. 【JAVA多线程】interrupted() 和 isInterrupted() 的区别

    Thread 类中提供了两种方法用来判断线程的状态是不是停止的.就是我们今天的两位主人公 interrupted() 和 isInterrupted() . interrupted() 官方解释:测试 ...

  6. 深入理解java虚拟机---虚拟机工具VisualVM(十九)

    性能分析神器VisualVM 9602 VisualVM 是一款免费的,集成了多个 JDK 命令行工具的可视化工具,它能为您提供强大的分析能力,对 Java 应用程序做性能分析和调优.这些功能包括生成 ...

  7. it网站

    1:http://www.importnew.com/    importnew  专注于java的论坛 2:Github是最活跃的开源代码库和版本控制平台,可以说是程序员当中知名度最高的技术社区.各 ...

  8. 数据分析常用的python工具和SQL语句

    select symbol, "price.*" from stocks :使用正则表达式来指定列查询 select count(*), avg(salary) from empl ...

  9. 24点小游戏app宣传文案

    24点小游戏app宣传文案 游戏背景 24点小游戏是传统的扑克牌游戏,是通过扑克牌来完成的竞争性智力游戏,除了希望能够消磨我们的空闲时间,加强同学们的临机和速算能力,还能够促进我们每个人的大脑和逻辑性 ...

  10. python 爬虫newspaper3k 新闻爬去方法 利用第三方库

    from newspaper import Article url = '你想要爬取的网站url' news = Article(url, language='zh') news .download( ...