Tomb Raider(暴力模拟)
Posted fighting-sh
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Tomb Raider(暴力模拟)相关的知识,希望对你有一定的参考价值。
Tomb Raider
https://hihocoder.com/problemset/problem/1829?sid=1394836
描述
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 }
以上是关于Tomb Raider(暴力模拟)的主要内容,如果未能解决你的问题,请参考以下文章
ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 B Tomb Raider 二进制枚举
cemetery,grave,tomb都是“坟墓”,有何区别?
鲁大师最新笔记本排行榜,微星 Raider GE76性能最强