Tomb Raider

题目描述:

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个字符串的LCS(Longest  Common Sequence)-最长公共字序列。

然后题目还给了一个要求,可以在任意位置开始。

这句话可以理解为,字符串是循环的,如 aabbcc-其中有一个字序列可以为:ccaabb.

意思就是循环字符串的最长公共字序列。

【题解】:

因为给定的字符串长度很小,最多才8.然后才十个字符串。

我们可以找出,第一个 字符串 的   所有的字序列 用回溯全部搜出来,先排序,然后一一匹配,

用一个n^2的复杂度匹配所有 其余 的字符串(预处理:首尾拼接-达到循环)。

贴上代码(队长ppr做法):

#include<bits/stdc++.h>
using namespace std;
string S[3000];
string str[20];
int cnt=0;
void printAllSub(string str, int i, string res)
{if (i == str.length()){if(res=="")return ;//cout << res << endl;S[cnt++]=res;return;}printAllSub(str, i + 1, res);         printAllSub(str, i + 1, res + str[i]);
}
bool cmp(string a,string b){if(a.size()==b.size()){return a<b;}else{return a.size()>b.size();}
}
int main()
{int n;while(~scanf("%d",&n)){cnt=0;/*if(n==0){break;}*/int len[20]={0};for(int i=0;i<n;i++){cin>>str[i];if(i!=0){len[i]=str[i].size();str[i]+=str[i];}}int t=str[0].size();string temp=str[0];str[0]+=str[0];for (int i=0;i<t;i++){string temp1=str[0].substr(i,t);//cout<<temp1<<endl;printAllSub(temp1,0,"");}sort(S,S+cnt,cmp);/*for(int i=0;i<cnt;i++){cout<<S[i]<<endl;}for(int i=1;i<n;i++){cout<<str[i]<<endl;}*/int num=0,tot=0;string ans="0";for(int i=0;i<cnt;i++){string tmp=S[i];tot=0;for(int j=1;j<n;j++){for(int k=0;k<len[j];k++){num=0;for(int L=k;L<len[j]+k;L++){if(str[j][L]==tmp[num]){num++;}if(num==tmp.size()){tot++;break;}}if(num==tmp.size()){break;}}}if(tot==n-1){ans=tmp;break;}}cout<<ans<<endl;}return 0;
}

贴上代码(自己做法):

#include<bits/stdc++.h>
using namespace std;
string S[3000];
string str[20];
map<string, int >mp[20];
int cnt=0;
void printAllSub(string str, int i, string res,int No)
{if (i == str.length()){if(res=="")return ;//cout << res << endl;if(No==0)S[cnt++]=res;mp[No][res]=1;return;}printAllSub(str, i + 1, res,No);printAllSub(str, i + 1, res + str[i],No);
}
bool cmp(string a,string b){if(a.size()==b.size()){return a<b;}else{return a.size()>b.size();}
}
int main()
{int n;while(~scanf("%d",&n)){for(int i=0;i<n;i++){mp[i].clear();}cnt=0;int len[20]={0};for(int i=0;i<n;i++){cin>>str[i];len[i]=str[i].size();str[i]+=str[i];}for(int i=0;i<n;i++){for (int j=0;j<len[i];j++){string temp=str[i].substr(j,len[i]);//cout<<temp1<<endl;printAllSub(temp,0,"",i);}}sort(S,S+cnt,cmp);/*for(int i=0;i<cnt;i++){cout<<S[i]<<endl;}for(int i=1;i<n;i++){cout<<str[i]<<endl;}*/int num=0,tot=0;string ans="0";for(int i=0;i<cnt;i++){int j;for(j=1;j<n;j++){if(mp[j][S[i]]==0){break;}}if(j==n){ans=S[i];break;}}cout<<ans<<endl;}return 0;
}

【18年北京网络赛】Tomb Raider【递归求所有子序列】相关推荐

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

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

  2. 2016 ICPC 北京网络赛 A 恶心模拟 F 循环矩阵,FFT(待补) I 模拟

    2016 ICPC 北京网络赛 A - The Book List 题意:每本书有所属种类,给出原生的存放方式,求按新的方式存放的样子. tags:坑到心态爆炸的题==  直接堆进vector里搞的, ...

  3. hihoCoder1233(2015北京网络赛H题)

    转载自:http://blog.csdn.net/queuelovestack/article/details/48625899 题意: 有n个卡槽,放有体积不同的n个空盒子,每次你可以移动一个空盒子 ...

  4. hihoCoder1228(2015北京网络赛B题)

    题意: 给出一个文本编辑器的容量,给出老板输入的字符串,小写字母代表文本,大写字母代表命令: L:光标左移: R:光标右移: S:在insert模式和另一个输入模式中切换: D:删除光标后面的一个字符 ...

  5. 2015北京网络赛 G题 Boxes bfs

    Boxes Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://hihocoder.com/contest/acmicpc2015beijingonl ...

  6. Saving Tang Monk II HihoCoder - 1828(2018北京网络赛三维标记+bfs)

    <Journey to the West>(also <Monkey>) is one of the Four Great Classical Novels of Chines ...

  7. HDU 5037 Frog(2014年北京网络赛 F 贪心)

    开始就觉得有思路,结果越敲越麻烦...  题意很简单,就是说一个青蛙从0点跳到m点,最多可以跳l的长度,原有石头n个(都仅表示一个点).但是可能跳不过去,所以你是上帝,可以随便在哪儿添加石头,你的策略 ...

  8. hihoCoder 1227 2015 北京网络赛 A题

    题意: 给定m个点,然后从这m个点里找到一个点作为圆心,求一个最小的半径使得圆内刚好有n个点,没有压线的点. 思路: 预处理一下各点间的距离,暴力枚举圆心,然后找到排序后的第n个判断即可.坑点:n&g ...

  9. 2017 ACM-ICPC北京网络赛: C. Matrix(DP)

    #1580 : Matrix 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Once upon a time, there was a little dog YK. On ...

  10. HihoCoder 1830 Cheat ICPC2018 北京网络赛

    //写完和贴吧群dalao的代码比了一下 结果发现总是有更好的写法让你规避bug - -郁闷https://paste.ubuntu.com/p/8RRBw7jrHQ/ #include <bi ...

最新文章

  1. 一款精品Android手电筒应用
  2. DRF——路由控制器
  3. Android Intent机制详解
  4. c语言中Gretchen函数的功能,听过很多的歌的音乐达人给我推荐一下
  5. 详解list容器(应用+模拟实现)
  6. VS2013提示错误应输入表达式
  7. c语言提取七位数讲解,C语言-体育彩票7位数,感受身中500万的fell
  8. html5 规定输入字段,HTML5 Input属性详解
  9. 谷歌力推新语言 Logica,解决 SQL 重大缺陷!
  10. 数据治理管理平台功能模块与特性
  11. 谷歌原数据保护团队技术主管:零信任实践分享
  12. 什么是项目管理43210法?
  13. 风靡健身圈的生酮饮食居然有这么多好处,受教了
  14. 使用BeautifulSoup,解释器报错‘lxml‘
  15. 清爽的VS开发字体 -- Consolas
  16. 使用蛮力法求解数字迷问题(类似ABCAB*A = DDDDDD)
  17. VIJOS-P1232核电站问题
  18. CPU个数、CPU核心数、CPU线程数
  19. Redis是什么?看这一篇就够了
  20. 历届NBA总决赛结果

热门文章

  1. 计算机无法连接网络打印机,网络打印机无法连接的解决方法是什么
  2. 黑暗森林定律:对费米悖论的解释
  3. 正点原子阿波罗STM32F7-红外遥控原理及代码
  4. 宇枫资本女性如何理财致富
  5. 香港自由行攻略(自用)
  6. ps4正在连接ea服务器,ps4极品飞车19连不上ea服务器 | 手游网游页游攻略大全
  7. linux中文输入配置sougou输入法
  8. 字节跳动Java金三银四解析:阿里巴巴技术专家之作
  9. 2018年中国网络游戏行业市场前景研究报告
  10. Camunda/Flowable/Activiti技术发展史/盘古BPM框架对比最新版