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 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.
Input
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.
Output
For each case, print the password. If there is no LCS, print 0 instead.
Sample Input
2
abcdefg
zaxcdkgb
5
abcdef
kedajceu
adbac
abcdef
abcdafc
2
abc
def
Sample Output
acdg
acd
0 题意:
每一组数据给你一个整数N,然后N个字符串,每一个字符串可以以字符串中的任意一个字符为起始字符(循环连接)
例如 asdb 可以变成 sdba
现在你可以任意变换这N这个字符串,求他们的最长的公共子序列LCS ,如果长度相等,请输出字典序最小的。 题意:
二进制枚举+暴力
我们对每一个字符串,我们枚举它的所有可能变成的字符串(只有字符串的长度数量的可能)。
然后对于每一个字符串我们二进制枚举它的所有可能子序列,然后用一个map<string,set<int> > m; 来维护。
对于每一个枚举出的子序列,把它加入map中,映射的set中加入当前子序列的编号,如果某一个子序列的set的大小是N,
那么说明 N 个字符串都有这个子序列,即N个字符串的公共子序列,然后选择长度最大,字典序最小的即可。 细节见code
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=;while(b){if(b%)ans=ans*a%MOD;a=a*a%MOD;b/=;}return ans;}
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
string str[];
string temp[];
int n;
map<string,set<int> > m;
string ans="";
void dfs(int id)
{
int len=str[id].length();
rep(i,,len)
{
temp[id]=str[id].substr(i,len-i)+str[id].substr(,i);
int xlen=temp[id].size();
string lcs="";
for(int i=;i<=(<<xlen)-;i++)
{
lcs="";
for(int j=;j<xlen;j++)
{
if((bool)(i&(<<j)))
{
lcs+=temp[id][j];
}
}
// db(lcs);
m[lcs].insert(id);
if(m[lcs].size()==n)
{
if(sz(lcs)>sz(ans))
{
ans=lcs;
}else if(sz(lcs)==sz(ans))
{
ans=min(ans,lcs);
}
}
}
}
if(id<n)
dfs(id+);
}
int main()
{
// freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
// freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
gbtb;
// cout<<(1<<8)<<endl;
// 8*256*10
while(cin>>n)
{
repd(i,,n)
{
cin>>str[i];
}
ans="";
m.clear();
dfs();
if(!sz(ans))
{
cout<<<<endl;
}else
{
cout<<ans<<endl;
}
} return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}
Tomb Raider HihoCoder - 1829 (二进制枚举+暴力)(The 2018 ACM-ICPC Asia Beijing First Round Online Contest)的更多相关文章
- Card Hand Sorting 二进制枚举暴力
这个题其实由于只有4种花色的,那么每种花色排列的顺序,也不过是4!种,然后对于每种花色内部到底是升序还是降序,其实也可以直接暴力,一共也就4!*2^4种情况,然后直接进行排序就可以了,但是我们如何计算 ...
- hihocoder 1388 &&2016 ACM/ICPC Asia Regional Beijing Online Periodic Signal
#1388 : Periodic Signal 时间限制:5000ms 单点时限:5000ms 内存限制:256MB 描述 Profess X is an expert in signal proce ...
- Hdu 5451 Best Solver (2015 ACM/ICPC Asia Regional Shenyang Online) 暴力找循环节 + 递推
题目链接: Hdu 5451 Best Solver 题目描述: 对于,给出x和mod,求y向下取整后取余mod的值为多少? 解题思路: x的取值为[1, 232],看到这个指数,我的心情是异常崩 ...
- ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 B Tomb Raider 【二进制枚举】
任意门:http://hihocoder.com/problemset/problem/1829 Tomb Raider 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 L ...
- ACM/ICPC 2018亚洲区预选赛北京赛站网络赛-B:Tomb Raider(二进制枚举)
时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Lara Croft, the fiercely independent daughter of a missing adv ...
- Tomb Raider(暴力模拟)
Tomb Raider https://hihocoder.com/problemset/problem/1829?sid=1394836 时间限制:1000ms 单点时限:1000ms 内存限制:2 ...
- ACM-ICPC2018北京网络赛 Tomb Raider(暴力)
题目2 : Tomb Raider 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Lara Croft, the fiercely independent daughte ...
- Consonant Fencity Gym - 101612C 暴力二进制枚举 Intelligence in Perpendicularia Gym - 101612I 思维
题意1: 给你一个由小写字母构成的字符串s,你可以其中某些字符变成大写字母.如果s中有字母a,你如果想把a变成大写,那s字符串中的每一个a都要变成A 最后你需要要出来所有的字符对,s[i]和s[i-1 ...
- Codeforces Round #272 (Div. 2) B. Dreamoon and WiFi (暴力二进制枚举)
题意:给你一个只含\(+\)和\(-\)的字符串,统计它的加减和,然后再给你一个包含\(+,-,?\)的字符串,其中\(?\)可以表示为\(+\)或\(-\),问有多少种情况使得第二个字符串的加减和等 ...
随机推荐
- 方差分析(python代码实现)
python机器学习-乳腺癌细胞挖掘(欢迎关注博主主页,学习python视频资源,还有大量免费python经典文章) https://study.163.com/course/introduction ...
- JS - 获取函数的参数名称
看 Angular 依赖注入时发现的神奇的操作,实现原理是 Function.prototype.toString() 得到函数的字符串然后用正则判断. 参见 javascript - How to ...
- 趣谈linux操作系统笔记-内核初始化
内核的启动从入口函数 start_kernel() 开始.在 init/main.c 文件中,start_kernel 相当于内核的main 函数.打开这个函数,你会发现,里面是各种各样初始化函数 X ...
- zabbix使用脚本监控
参照:http://www.cnblogs.com/zhongkai-27/p/9984597.html
- 使用Jquery的Ajax调用
最近在学习web开发,试用了一下Jquery的ajax调用. 首先,新建一个MVC4的项目,在HomeController.cs中添加一个Action,命名为GetData, 通过这个为ajax提供数 ...
- 【HANA系列】SAP HANA的ini文件存储路径
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA的ini文件存储 ...
- 用番茄工作法提升工作效率 (四)ToDoList的持续优化
一.写在前面 前面三篇文章,系统介绍了我如何使用番茄工作法,并结合“自制”的桌面ToDoList工具来实现自己的任务管理. 自制ToDoList的初衷是自我管理,但是好友看到我的桌面(程序)后,建议我 ...
- 【神经网络与深度学习】leveldb的实现原理
郑重声明:本篇博客是自己学习 Leveldb 实现原理时参考了郎格科技系列博客整理的,原文地址:http://www.samecity.com/blog/Index.asp?SortID=12,只是为 ...
- linux环境下编译Android apk
Android源码目录下的build/envsetup.sh文件,描述编译的命令 - m: Makes from the top of the tree. - mm: Build ...
- form 源码刨析
def clean_name(self) value = self.cleaned_data.get('name') if "金-瓶-梅" not in value: raise ...