Tomb Raider(暴力模拟)

Tomb Raider

https://hihocoder.com/problemset/problem/1829?sid=1394836

时间限制: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


求所有字符串的公共的LCS,因为数据范围很小,所以可以把第一个字符串的所有子序列求出来,和剩余的一个一个比较
注意,n是大于0的,所以有可能只有一个字符串。因为被看清这点,多调了半小时。。。
 1 #include<iostream>
 2 #include<cstring>
 3 #include<string>
 4 #include<cmath>
 5 #include<algorithm>
 6 #include<queue>
 7 #include<cstdio>
 8 using namespace std;
 9 
10 string s[15];
11 string ans[305];
12 int main(){
13 
14     int n;
15     string str;
16     while(cin>>n){
17         for(int i=1;i<=n;i++){
18             cin>>s[i];
19         }
20         int coun=0;
21         int len=s[1].length();
22         int num=1<<len;
23         for(int i=1;i<num;i++){
24             int j=i;
25             str="";
26             for(int k=0;k<len;k++){
27                 if((j>>k)&1){
28                     str+=s[1][k];
29                 }
30             }
31             int co=0;
32             for(int k=1;k<=n;k++){
33                 for(int h=0;h<s[k].length();h++){
34                     int len1=str.length();
35                     int len2=s[k].length();
36                     int l1=0,l2=0;
37                     while(l1<len1&&l2<len2){
38                         if(str[l1]==s[k][l2]){
39                             l1++;
40                             l2++;
41                         }
42                         else{
43                             l2++;
44                         }
45                     }
46                     if(l1==len1){
47                         co++;
48                         break;
49                     }
50                     char hh=s[k][0];
51                     s[k]=s[k].substr(1);
52                     s[k]+=hh;
53                 }
54                 if(co==n){
55                     ans[coun++]=str;
56                 }
57             }
58         }
59         int prelen=0;
60         if(coun==0){
61             cout<<0<<endl;
62         }
63         else{
64             for(int i=0;i<coun;i++){
65                 if(prelen<ans[i].length()){
66                     prelen=ans[i].length();
67                 }
68             }
69             string aa;
70 
71             for(int i=0;i<coun;i++){
72                 if(prelen==ans[i].length()){
73                     aa=ans[i];
74                     break;
75                 }
76             }
77             string pre="zzzzzzzzzzzz";
78             for(int i=0;i<aa.length();i++){
79                 char tmp=aa[0];
80                 aa=aa.substr(1);
81                 aa+=tmp;
82                 if(pre>aa){
83                     pre=aa;
84                 }
85             }
86             cout<<pre<<endl;
87         }
88 
89     }
90 
91 }
View Code

 

 

转载于:https://www.cnblogs.com/Fighting-sh/p/9718042.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值