任意门: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:

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <cstring>
 5 using namespace std;
 6
 7 int len[15], len_ans;
 8 char s[100+5][80+5], ans[80+5];
 9
10 bool judge(char* ss, int k)
11 {
12     for(int f = 0; f < len[k]; f++){
13         int pss = 0;
14         for(int i = 0; i < len[k]; i++){     //逐位寻找
15             if(s[k][f+i] == ss[pss]){
16                 pss++;
17                 if(ss[pss] == '\0') return true; //所有都能找到,满足条件
18             }
19         }
20     }
21     return false;
22 }
23
24 void check(int n)
25 {
26     char tss[20+5], css[20+5];
27     int u = 1<<len[0];                      //二进制每一位代表一个字符
28     for(int f = 0; f < len[0]; f++){        //枚举不同起点的子串
29         strncpy(tss, s[0]+f, len[0]);       // f 为起点
30         tss[len[0]] = '\0';
31         for(int k = 1; k < u; k++){         //二进制枚举当前起点的子串的子串
32             int p = 0;
33             for(int i = 0; i < len[0]; i++){
34                 if(!(k&(1<<i))) continue;
35                 css[p] = tss[i];
36                 p++;
37             }
38             css[p] = '\0';
39             bool ok = true;                 //判断其他字符环是否也存在这个子串
40             for(int i = 1; i < n; i++){
41                 if(!judge(css, i)){         //有一个不满足就匹配失败
42                     ok = false;break;
43                 }
44             }
45             if(ok){                         //如果当前子串满足条件
46                 if(len_ans == -1){          //当前子串为找到的第一个满足所有条件的子串
47                     strcpy(ans, css);
48                     len_ans = strlen(ans);
49                 }
50                 else{
51                     int lencss = strlen(css);
52                     if(lencss > len_ans){           //比较子串长度,长的优先
53                         strcpy(ans, css);
54                         len_ans = lencss;
55                     }
56                     else if(len_ans == lencss){     //长度相同,字典序小的优先
57                         if(strcmp(ans, css) > 0){
58                             strcpy(ans, css);
59                         }
60                     }
61                 }
62             }
63         }
64     }
65 }
66
67 int main()
68 {
69     int N;
70     char tmp[20+5];
71     while(~scanf("%d", &N)){
72         for(int i = 0; i < N; i++){
73             scanf("%s", &s[i]);
74             len[i] = strlen(s[i]);
75             strcpy(tmp, s[i]);      //字符串double 处理环
76             strcat(s[i], tmp);
77         }
78         len_ans = -1;               //答案字符长度
79         check(N);                       //暴力
80         if(len_ans == -1) puts("0");    //没有满足条件的字串
81         else printf("%s\n", ans);
82     }
83     return 0;
84 }

View Code

ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 B Tomb Raider 【二进制枚举】相关推荐

  1. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 【bfs + 记忆化搜索 + 剪枝】 AC 代码

    ACM 北京区域赛 bfs+剪枝+ms 第一个一遍过的题目,基本没有看题解 记忆搜索当中,注意初始化成一个特殊值:而在访问之后,每个点就会有一个不同于INF(或者 -1等特殊标记)的值 先到先得,适者 ...

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

    #1829 : Tomb Raider 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Lara Croft, the fiercely independent daugh ...

  3. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 80 Days(双向队列+尺取法)

    #1831 : 80 Days 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 80 Days is an interesting game based on Jules ...

  4. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 D【队列】

    #1831 : 80 Days 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 80 Days is an interesting game based on Jules ...

  5. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 D. 80 Days

    题解 题目大意 n个点组成一个环形 初始钱为m 从i走到j需要-b[i] + a[j] 要求按照顺时针走完所有的点(不用再回到起点) 过程中m不能小于0 输出最小的起点编号 直接把a[i]和b[i]合 ...

  6. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛

    Saving Tang Monk II Tomb Raider Cheat 80 Days Odd Chess Shortest Path Problem The Mole K-Dimensional ...

  7. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 A Saving Tang Monk II【分层bfs】

    时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 <Journey to the West>(also <Monkey>) is one of the ...

  8. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 A. Saving Tang Monk II

    题解 题目大意 给一个图S是起点 T是终点 .是空房间 #是毒气室 B是氧气瓶存放室 P是加速室 每次走到空房间或者起点消耗1秒 走到氧气室获得一个氧气瓶最多携带5个氧气瓶 进入毒气室需要一瓶氧气并且 ...

  9. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 80 Days ——尺取

    描述 80 Days is an interesting game based on Jules Verne's science fiction "Around the World in E ...

  10. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 C-cheat

    超级水的模拟的,我也就会做模拟了--坑有点多,一直没调出来bug,赛后才发现少了一个数字-- 描述 Cheat is a card game played by four players sittin ...

最新文章

  1. Spring Mock单元测试
  2. 如何优雅的关闭容器,看这一篇就够了
  3. 如何估算PGA,SGA的大小,配置数据库服务器的内存
  4. android自定义抽奖,Android自定义view制作抽奖转盘
  5. PostgreSQL 10.1 手册_部分 II. SQL 语言_第 11 章 索引_11.5. 组合多个索引
  6. 2016-12-31:最后一天:回顾
  7. Java设计模式、框架、架构、平台之间的关系
  8. (standard input): No keywords in input file
  9. 重载,重写(覆盖)和隐藏的区别
  10. python哈希类型_Python散列类型和运算符
  11. CodeForces 128A Statues 简单搜索
  12. 问卷设计中 你经常使用计算机吗,计算机应用基础课程调查问卷
  13. 在线代理(Web ProxyServer)完全详解
  14. 超图软件裁剪倾斜数据
  15. 从球域采样分布分析360质量评估
  16. 代理是如何实现IP伪装的呢?
  17. 杜甫写的有关风雨的古诗有哪些
  18. 第11章 运算符重载与约定
  19. lwm2m和coap协议 简解读
  20. 新型肺炎疫情期间,有哪些服务免费开放?

热门文章

  1. 文献检索——Web of Science|CSDN创作打卡
  2. 蓝桥杯 算法训练 ALGO-114 黑白无常
  3. 四.驱动框架入门之LED(中)
  4. hz什么梗_hz是什么意思饭圈
  5. Canvas线条动画
  6. 网页里面嵌入视频代码
  7. C# Activator.CreateInstance()方法使用
  8. unity游戏开发之游戏过审后 国行PS4将在3月20日发售
  9. 网站点击量太大崩溃怎么办_网站崩溃时该怎么办
  10. 电压力锅中的计算机控制系统,电压力锅的(电脑板)工作原理