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集合类,详解
集合详解 1.1HashSet HashSet是Set接口的一个子类,主要的特点是:里面不能存放重复元素,而且采用散列的存储方法,所以没有顺序.这里所说的没有顺序是指:元素插入的顺序与输出的顺序不一致 ...
- linux 添加php gd扩展 (linux添加PHP扩展)
首先最基本的 第一:先安装库 yum -y install libjpeglibjpeg-devel libpng libpng-devel freetype freetype-devel 第二:进入 ...
- MySQl 主从配置实战
目前后台数据库使用了一个实例做数据统计分析,随着数据井喷,单个实例无法做数据分析.故开始了读写分离. 1.主配置 [client] port = 3306 socket = /tmp/mysql-33 ...
- 每天CSS学习之white-space
white-space是CSS的属性,其作用是规定文本不进行换行. white-space有以下几个值: 1.normal:该值为默认值,段落前后的空白会被浏览器忽略.如下所示: <div st ...
- [leetcode53]最长子数组 Maximum Subarray Kadane's算法
[题目] Given an integer array nums, find the contiguous subarray (containing at least one number) whic ...
- 谈谈你对Java面向对象的理解
面向对象,其实是一种思考的思想,是一种思想,而这种思想它早期的思想是面向过程,通过不断的演化变成了现在的面向对象,思想有一个演变形式,早期是面向过程,现在是面向对象. 故事:把大象放进冰箱里,分几步? ...
- SQL-8 找出所有员工当前(to_date='9999-01-01')具体的薪水salary情况,对于相同的薪水只显示一次,并按照逆序显示
题目描述 找出所有员工当前(to_date='9999-01-01')具体的薪水salary情况,对于相同的薪水只显示一次,并按照逆序显示CREATE TABLE `salaries` (`emp_n ...
- bootstrap table 列求和
<div class="modal fade in" id="_modalDialog" tabindex="1" role=&quo ...
- 安装google 框架
使用 root exporer很方便 su cp /sdcard/google/busybox /data/local/tmp chmod 0755 /data/local/tmp/busybo ...
- SharePoint REST API - 基本操作(二)
博客地址:http://blog.csdn.net/FoxDave 上一节讲了SharePoint REST API的一些基本操作,本节将继续介绍一些关于SharePoint REST API的内容. ...