ACM/ICPC 2018亚洲区预选赛北京赛站网络赛-B:Tomb Raider(二进制枚举)
时间限制: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(二进制枚举)的更多相关文章
- ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 B Tomb Raider 【二进制枚举】
任意门:http://hihocoder.com/problemset/problem/1829 Tomb Raider 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 L ...
- ACM/ICPC 2018亚洲区预选赛北京赛站网络赛D-80 Days--------树状数组
题意就是说1-N个城市为一个环,最开始你手里有C块钱,问从1->N这些城市中,选择任意一个,然后按照顺序绕环一圈,进入每个城市会有a[i]元钱,出来每个城市会有b[i]个城市,问是否能保证经过每 ...
- ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 80 Days(尺取)题解
题意:n个城市,初始能量c,进入i城市获得a[i]能量,可能负数,去i+1个城市失去b[i]能量,问你能不能完整走一圈. 思路:也就是走的路上能量不能小于0,尺取维护l,r指针,l代表出发点,r代表当 ...
- ACM/ICPC 2018亚洲区预选赛北京赛站网络赛
题意:到一个城市得钱,离开要花钱.开始时有现金.城市是环形的,问从哪个开始,能在途中任意时刻金钱>=0; 一个开始指针i,一个结尾指针j.指示一个区间.如果符合条件++j,并将收益加入sum中( ...
- hihoCoder #1831 : 80 Days-RMQ (ACM/ICPC 2018亚洲区预选赛北京赛站网络赛)
水道题目,比赛时线段树写挫了,忘了RMQ这个东西了(捞) #1831 : 80 Days 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 80 Days is an int ...
- ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 A、Saving Tang Monk II 【状态搜索】
任意门:http://hihocoder.com/problemset/problem/1828 Saving Tang Monk II 时间限制:1000ms 单点时限:1000ms 内存限制:25 ...
- 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, ...
- ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 D 80 Days (线段树查询最小值)
题目4 : 80 Days 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 80 Days is an interesting game based on Jules Ve ...
- hihoCoder-1829 2018亚洲区预选赛北京赛站网络赛 B.Tomb Raider 暴力 字符串
题面 题意:给你n个串,每个串都可以选择它的一个长度为n的环形子串(比如abcdf的就有abcdf,bcdfa,cdfab,dfabc,fabcd),求这个n个串的这些子串的最长公共子序列(每个串按顺 ...
随机推荐
- Java Web(八) 事务,安全问题及隔离级别
事务 什么是事务? 事务就是一组原子性的SQL查询,或者说是一个独立的工作单元. 事务的作用 事务在我们平常的CRUD(增删改查)操作当中也许不太常用, 但是如果我们有一种需求,一组操作中必须全部成功 ...
- AI工具(矩形工具)(椭圆工具的操作与矩形类似)(剪切蒙版)5.11
矩形工具:按住SHIFT键,可以绘制一个正方形. 按住ALT键,可以绘制以落点为中心的矩形. 同时按住SHIFT和ALT键可以绘制以鼠标落点为中心的正方形. 选择矩形工具,点击页面,输入高宽,精确绘制 ...
- laravel模型表建立外键约束的使用:
模型: //表->posts class Post extends Model { //关联用户: public function user(){ //belongsTo,第一个参数:外键表,第 ...
- mysql插入中文乱码
https://www.cnblogs.com/zhchoutai/p/7364835.html 最简单的一招,不用修改my.ini文件: 1.停掉mysql服务 2.启动:X:\%path%\MyS ...
- MATLAB 批量处理图片
function resizephotos(directory, wh, isrecursive, isoverwrite, savetopath, supportFormat) % resizeph ...
- C++基础知识:泛型编程
1.泛型编程的概念 ---不考虑具体数据类型的编程模式Swap 泛型写法中的 T 不是一个具体的数据类型,而是泛指任意的数据类型. 2.函数模板 - 函数模板其实是一个具有相同行为的函数家族,可用不同 ...
- 解决NPM无法安装任何包的解决方案(npm ERR! code MODULE_NOT_FOUND)
前言 今天突然发现npm无法使用了,执行任何命令都报如下错误: npm ERR! code MODULE_NOT_FOUND npm ERR! Cannot find module 'internal ...
- easyui 日期控件限制起始相差30天
$('#lendDateStart').datebox('calendar').calendar({ validator: function(date){ var endDateStr = $('#l ...
- rabbitMq无法消费发送的q的问题
1.问题叙述: 该项目配置了10来个mq,应对新开发需求,我也加了一个mq配置,然后在本地代码当中调用,当中接受,与前面写法相似,项目上测试环境测试.发现发送了queue之后本地消费日志没有的bug. ...
- 安装gcc
yum -y install gcc yum -y install gcc-c++ yum install make -- 或者 yum groupinstall "Developmen ...