ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 hihocoder #1829 : Tomb Raider(模拟)

                                             题目2 : 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的范围很小只有10,并且字符的长度也很短只有8.所以大胆的暴力。用DFS处理出两个字符串中的所有公共子序列,用一个队列来保存,然后每次操作只取出原本在队列里子序列数量个字符串来DFS。总之就是一通瞎搞。。。

 

四、代码

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
vectorq;
mapmmp,vis;
string a1, a2;
int len1, len2;
string ret;
void DFS(int xx,int yy)
{
	for (int i = xx; i < len1; i++)
	{
		for (int j = yy; j < len2; j++)
		{
			if (a1[i] == a2[j])
			{
				ret += a1[i];
				vis[ret]++;
				DFS(i + 1, j + 1);
				ret.pop_back();
			}
		}
	}

}
void solve() 
{
	queueque;
	for (int i = 0; i <= q[0].length() - 1; i++) 
	{
		char c = q[0][0];
		q[0].erase(0, 1);
		q[0] += c;
			vis.clear();
			ret = "";
			a1 = q[0];
			a2 = q[1];
			len1 = a1.length();
			len2 = a2.length();
			DFS(0, 0);
			map::iterator it = vis.begin(),end=vis.end();
			for (; it != end; it++)
			{
				mmp[it->first]++;
			}
	}
	map::iterator it = mmp.begin(), end = mmp.end();
	for (; it != end; it++)
	{
		string nx = it->first;
		for (int j = 0; j <= nx.size() - 1; j++)
		{
			char tt = nx[0];
			nx.erase(0, 1);
			nx += tt;
			que.push(nx);
		}
	}
		
	for (int i = 2; i < q.size(); i++)
	{
		int S = que.size();
		for (int cnt = 1; cnt <= S; cnt++)
		{
			vis.clear();
			mmp.clear();
			ret = "";
			string tt = que.front();
			que.pop();
			a1 = tt;
			a2 = q[i];
			len1 = a1.length();
			len2 = a2.length();
			DFS(0, 0);
			map::iterator it = vis.begin(), end = vis.end();
			for (; it != end; it++)
			{
				mmp[it->first]++;
			}
			it = mmp.begin(), end = mmp.end();
			for (; it != end; it++)
			{
				string nx = it->first;
				for (int j = 0; j <= nx.size() - 1; j++)
				{
					char tt = nx[0];
					nx.erase(0, 1);
					nx += tt;
					que.push(nx);
				}
			}


		}
	}
	bool tag = false;
	int maxx = 0;
	string ans="";
	while (!que.empty())
	{
		string TT = que.front();
		que.pop();
		int lll = TT.length();
		if (lll > maxx)
		{
			maxx = lll;
			ans = TT;
			tag = true;
		}
		else if (lll == maxx)
		{
			for (int i = 0; i <= TT.size() - 1; i++)
			{
				char tt = TT[0];
				TT.erase(0, 1);
				TT += tt;
				if (TT < ans)
				{
					ans = TT;
				}
			}
		}
	}
	if (tag)
		cout << ans << endl;
	else
		cout << 0 << endl;




}
int main(void) {
	int n;
	while (cin >>n) {
		mmp.clear();
		q.clear();
		for (int i = 0; i < n; i++) {
			string a;
			cin >> a;
			q.push_back(a);
		}
		if (n == 1) 
		{
			string ans = q[0];
			for (int i = 0; i <= q[0].size() - 1; i++)
			{
				char tt = q[0][0];
				q[0].erase(0, 1);
				q[0] += tt;
				if (q[0] < ans)
					ans = q[0];

			}
			cout << ans << endl;
			continue;
		}
		solve();
	}
}

 

你可能感兴趣的:(ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 hihocoder #1829 : Tomb Raider(模拟))