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

  1. 2
  2. abcdefg
  3. zaxcdkgb
  4. 5
  5. abcdef
  6. kedajceu
  7. adbac
  8. abcdef
  9. abcdafc
  10. 2
  11. abc
  12. def

Sample Output

  1. acdg
  2. acd
  3. 0
  4.  
  5. 题意:
    每一组数据给你一个整数N,然后N个字符串,每一个字符串可以以字符串中的任意一个字符为起始字符(循环连接)
    例如 asdb 可以变成 sdba
    现在你可以任意变换这N这个字符串,求他们的最长的公共子序列LCS ,如果长度相等,请输出字典序最小的。
  6.  
  7. 题意:
    二进制枚举+暴力
    我们对每一个字符串,我们枚举它的所有可能变成的字符串(只有字符串的长度数量的可能)。
    然后对于每一个字符串我们二进制枚举它的所有可能子序列,然后用一个map<string,set<int> > m; 来维护。
    对于每一个枚举出的子序列,把它加入map中,映射的set中加入当前子序列的编号,如果某一个子序列的set的大小是N
    那么说明 N 个字符串都有这个子序列,即N个字符串的公共子序列,然后选择长度最大,字典序最小的即可。
  8.  
  9. 细节见code
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <queue>
  7. #include <stack>
  8. #include <map>
  9. #include <set>
  10. #include <vector>
  11. #include <iomanip>
  12. #define ALL(x) (x).begin(), (x).end()
  13. #define rt return
  14. #define dll(x) scanf("%I64d",&x)
  15. #define xll(x) printf("%I64d\n",x)
  16. #define sz(a) int(a.size())
  17. #define all(a) a.begin(), a.end()
  18. #define rep(i,x,n) for(int i=x;i<n;i++)
  19. #define repd(i,x,n) for(int i=x;i<=n;i++)
  20. #define pii pair<int,int>
  21. #define pll pair<long long ,long long>
  22. #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
  23. #define MS0(X) memset((X), 0, sizeof((X)))
  24. #define MSC0(X) memset((X), '\0', sizeof((X)))
  25. #define pb push_back
  26. #define mp make_pair
  27. #define fi first
  28. #define se second
  29. #define eps 1e-6
  30. #define gg(x) getInt(&x)
  31. #define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
  32. using namespace std;
  33. typedef long long ll;
  34. ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
  35. ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
  36. 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;}
  37. inline void getInt(int* p);
  38. const int maxn=;
  39. const int inf=0x3f3f3f3f;
  40. /*** TEMPLATE CODE * * STARTS HERE ***/
  41. string str[];
  42. string temp[];
  43. int n;
  44. map<string,set<int> > m;
  45. string ans="";
  46. void dfs(int id)
  47. {
  48. int len=str[id].length();
  49. rep(i,,len)
  50. {
  51. temp[id]=str[id].substr(i,len-i)+str[id].substr(,i);
  52. int xlen=temp[id].size();
  53. string lcs="";
  54. for(int i=;i<=(<<xlen)-;i++)
  55. {
  56. lcs="";
  57. for(int j=;j<xlen;j++)
  58. {
  59. if((bool)(i&(<<j)))
  60. {
  61. lcs+=temp[id][j];
  62. }
  63. }
  64. // db(lcs);
  65. m[lcs].insert(id);
  66. if(m[lcs].size()==n)
  67. {
  68. if(sz(lcs)>sz(ans))
  69. {
  70. ans=lcs;
  71. }else if(sz(lcs)==sz(ans))
  72. {
  73. ans=min(ans,lcs);
  74. }
  75. }
  76. }
  77. }
  78. if(id<n)
  79. dfs(id+);
  80. }
  81. int main()
  82. {
  83. // freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
  84. // freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
  85. gbtb;
  86. // cout<<(1<<8)<<endl;
  87. // 8*256*10
  88. while(cin>>n)
  89. {
  90. repd(i,,n)
  91. {
  92. cin>>str[i];
  93. }
  94. ans="";
  95. m.clear();
  96. dfs();
  97. if(!sz(ans))
  98. {
  99. cout<<<<endl;
  100. }else
  101. {
  102. cout<<ans<<endl;
  103. }
  104. }
  105.  
  106. return ;
  107. }
  108.  
  109. inline void getInt(int* p) {
  110. char ch;
  111. do {
  112. ch = getchar();
  113. } while (ch == ' ' || ch == '\n');
  114. if (ch == '-') {
  115. *p = -(getchar() - '');
  116. while ((ch = getchar()) >= '' && ch <= '') {
  117. *p = *p * - ch + '';
  118. }
  119. }
  120. else {
  121. *p = ch - '';
  122. while ((ch = getchar()) >= '' && ch <= '') {
  123. *p = *p * + ch - '';
  124. }
  125. }
  126. }
  1.  

Tomb Raider HihoCoder - 1829 (二进制枚举+暴力)(The 2018 ACM-ICPC Asia Beijing First Round Online Contest)的更多相关文章

  1. Card Hand Sorting 二进制枚举暴力

    这个题其实由于只有4种花色的,那么每种花色排列的顺序,也不过是4!种,然后对于每种花色内部到底是升序还是降序,其实也可以直接暴力,一共也就4!*2^4种情况,然后直接进行排序就可以了,但是我们如何计算 ...

  2. 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 ...

  3. Hdu 5451 Best Solver (2015 ACM/ICPC Asia Regional Shenyang Online) 暴力找循环节 + 递推

    题目链接: Hdu  5451  Best Solver 题目描述: 对于,给出x和mod,求y向下取整后取余mod的值为多少? 解题思路: x的取值为[1, 232],看到这个指数,我的心情是异常崩 ...

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

    任意门:http://hihocoder.com/problemset/problem/1829 Tomb Raider 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 L ...

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

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

  6. Tomb Raider(暴力模拟)

    Tomb Raider https://hihocoder.com/problemset/problem/1829?sid=1394836 时间限制:1000ms 单点时限:1000ms 内存限制:2 ...

  7. ACM-ICPC2018北京网络赛 Tomb Raider(暴力)

    题目2 : Tomb Raider 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Lara Croft, the fiercely independent daughte ...

  8. Consonant Fencity Gym - 101612C 暴力二进制枚举 Intelligence in Perpendicularia Gym - 101612I 思维

    题意1: 给你一个由小写字母构成的字符串s,你可以其中某些字符变成大写字母.如果s中有字母a,你如果想把a变成大写,那s字符串中的每一个a都要变成A 最后你需要要出来所有的字符对,s[i]和s[i-1 ...

  9. Codeforces Round #272 (Div. 2) B. Dreamoon and WiFi (暴力二进制枚举)

    题意:给你一个只含\(+\)和\(-\)的字符串,统计它的加减和,然后再给你一个包含\(+,-,?\)的字符串,其中\(?\)可以表示为\(+\)或\(-\),问有多少种情况使得第二个字符串的加减和等 ...

随机推荐

  1. docker-compose部署redis-cluster

    node1-node6 1.到http://download.redis.io/redis-stable/redis.conf下载官方redis.conf文件 2.修改对应端口 # bind 127. ...

  2. Mini-batch 和batch的区别

    原文地址:https://blog.csdn.net/weixin_39502247/article/details/80032487 深度学习的优化算法,说白了就是梯度下降.每次的参数更新有两种方式 ...

  3. svn版本合并

    假如你的项目(这里指的是手机客户端项目)的某个版本(例如1.0版本)已经完成开发.测试并已经上线了,接下来接到新的需求,新需求的开发需要修改多个文件中的代码,当需求已经开始开发一段时间的时候,突然接到 ...

  4. video标签在移动端的一些属性值设置

    <video x5-video-orientation="portraint" src="" loop x-webkit-airplay="al ...

  5. 清晰理解redux中的

    首先需要明白 Redux 的单一状态树的概念,所谓的单一状态树,就是指“所有的 state 都以一个对象树的形式储存在一个单一的 store 中.” 比如我们有这么一个状态树(或者你叫它状态对象也行) ...

  6. APP Store上架QA&注意事项

    一. App Store上架费用,要多少钱. 这个因产品而异,一般是6000-10000元人民币. 二. App Store上架周期,要多久过. 这个因产品而异,正常的话一周内,如果产品老是出问题,被 ...

  7. Docker面试题(二)

    什么是虚拟化? 虚拟化允许您在相同的硬件上运行两个完全不同的操作系统.每个客户操作系统都经历了引导,加载内核等所有过程.您可以拥有非常严格的安全性,例如,客户操作系统无法完全访问主机操作系统或其他客户 ...

  8. 【VS开发】使用WinPcap编程(2)——打开网络设备并且开始捕获数据包

    这里需要特别强调的一个数据结构是pcap_t,它相当于一个文件描述符,代表一个已经打开的设备.我们对这个设备进行操作,就是对这个文件描述符进行操作. 首先是打开一个已知的设备,使用pcap_open( ...

  9. redis学习(三)

    如何保障reids的数据安全和性能?   一.持久化选项 1.快照snapshotting 它可以将存在于某一时刻的所有数据都写入硬盘里面. 配置选项示例: save 60 1000 注:从最近一次创 ...

  10. layer最大话.最小化.还原回调方法

    layer.open({              type: 1,             title: ‘在线调试‘,           content: ‘这里是内容‘,            ...