任意门:http://hihocoder.com/problemset/problem/1829

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 个字符环的公共子序列(只考虑顺时针方向)

解题思路:

一开始是想两两配对出最长子序列再与下一个配对,用一个队列来实现字符环子序列的操作。

不过这显然是错误的做法,因为两两配对的最长子序列可能存在多种情况。

正确的打开方式:字串全部暴力一遍!!!

具体的暴力方法是二进制枚举,只暴力第一个字符环,把暴力出来的子串与剩下的字符环配对,判断是否每个字符环都存在这个子串, 如果存在保留最长的,如果相同长度保留字典序最小的(直接strcmp()就可以判断哪个子串字典序更小了)。

而处理环的方法是最常见的 double 字符串,两个相同字符串首尾相接就成环了(废话。。。)。

AC code:

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std; int len[], len_ans;
char s[+][+], ans[+]; bool judge(char* ss, int k)
{
for(int f = ; f < len[k]; f++){
int pss = ;
for(int i = ; i < len[k]; i++){ //逐位寻找
if(s[k][f+i] == ss[pss]){
pss++;
if(ss[pss] == '\0') return true; //所有都能找到,满足条件
}
}
}
return false;
} void check(int n)
{
char tss[+], css[+];
int u = <<len[]; //二进制每一位代表一个字符
for(int f = ; f < len[]; f++){ //枚举不同起点的子串
strncpy(tss, s[]+f, len[]); // f 为起点
tss[len[]] = '\0';
for(int k = ; k < u; k++){ //二进制枚举当前起点的子串的子串
int p = ;
for(int i = ; i < len[]; i++){
if(!(k&(<<i))) continue;
css[p] = tss[i];
p++;
}
css[p] = '\0';
bool ok = true; //判断其他字符环是否也存在这个子串
for(int i = ; i < n; i++){
if(!judge(css, i)){ //有一个不满足就匹配失败
ok = false;break;
}
}
if(ok){ //如果当前子串满足条件
if(len_ans == -){ //当前子串为找到的第一个满足所有条件的子串
strcpy(ans, css);
len_ans = strlen(ans);
}
else{
int lencss = strlen(css);
if(lencss > len_ans){ //比较子串长度,长的优先
strcpy(ans, css);
len_ans = lencss;
}
else if(len_ans == lencss){ //长度相同,字典序小的优先
if(strcmp(ans, css) > ){
strcpy(ans, css);
}
}
}
}
}
}
} int main()
{
int N;
char tmp[+];
while(~scanf("%d", &N)){
for(int i = ; i < N; i++){
scanf("%s", &s[i]);
len[i] = strlen(s[i]);
strcpy(tmp, s[i]); //字符串double 处理环
strcat(s[i], tmp);
}
len_ans = -; //答案字符长度
check(N); //暴力
if(len_ans == -) puts(""); //没有满足条件的字串
else printf("%s\n", ans);
}
return ;
}

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

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

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

  2. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛-B:Tomb Raider(二进制枚举)

    时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Lara Croft, the fiercely independent daughter of a missing adv ...

  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. Laplace变换要点

    Laplace变换在求解微分方程.信号系统.自动控制领域都有重要作用.阅读<复变函数与积分变换>华中科大第三版,小结要点. 方便应用,最先给出变换表: 定义: 性质: 周期函数与卷积:

  2. oracle 基础(一)--闪回技术

    一,闪回表初探 闪回须知: 1 使用闪回表注意如下事项: 2 3 (1)被闪回的表必须启用行移动功能 4 5 SQL> alter table dept enable row movement; ...

  3. 牛客网Java刷题知识点之拥塞发生的主要原因、TCP拥塞控制、TCP流量控制、TCP拥塞控制的四大过程(慢启动、拥塞避免、快速重传、快速恢复)

    不多说,直接上干货! 福利 => 每天都推送 欢迎大家,关注微信扫码并加入我的4个微信公众号:   大数据躺过的坑      Java从入门到架构师      人工智能躺过的坑          ...

  4. ElasticSearch 2.1.1学习及总结

    Install & Up cd elasticsearch-2.1.1/bin ./elasticsearch ./elasticsearch --cluster.name my_cluste ...

  5. HAProxy advanced Redis health check---ref

    http://blog.exceliance.fr/2014/01/02/haproxy-advanced-redis-health-check/ HAProxy advanced Redis hea ...

  6. HDU 1069—— Monkey and Banana——————【dp】

    Monkey and Banana Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  7. tomcat-dbcp数据库连接池配置以及使用时候的一些坑

    一.数据库连接池 开发的时候经常会需要对数据库进行一些操作,比如说常见的增删改查之类的,当数据量小的时候,可以直接进行操作,但是当数据量增多的时候,每一次连接以及释放数据库都会耗费一定的时间,这个时候 ...

  8. 获取memcache中所有数据

    remap_table方法是用的一个框架写的: $gvs = $this->pageObj->get;是获取通过get方式传递过来的数据: $mem = $this->pageObj ...

  9. [LeetCode]21. Merge Two Sorted Lists合并两个有序链表

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  10. python的返回值

    1.返回值的作用 函数并非总是直接显示输出,相反,它可以处理一些数据,并返回一个或一组值.函数返回的值被称为返回值.在函数中,可使用return语句将值返回到调用函数的代码行.返回值让你能够将程序的大 ...