B - Suffix Structures(CF,255)

B - Suffix Structures 

Bizon the Champion isn't just a bison. He also is a favorite of the "Bizons" team.

At a competition the "Bizons" got the following problem: "You are given two distinct words (strings of English letters), s and t. You need to transform word s into word t". The task looked simple to the guys because they know the suffix data structures well. Bizon Senior loves suffix automaton. By applying it once to a string, he can remove from this string any single character. Bizon Middle knows suffix array well. By applying it once to a string, he can swap any two characters of this string. The guys do not know anything about the suffix tree, but it can help them do much more.

Bizon the Champion wonders whether the "Bizons" can solve the problem. Perhaps, the solution do not require both data structures. Find out whether the guys can solve the problem and if they can, how do they do it? Can they solve it either only with use of suffix automaton or only with use of suffix array or they need both structures? Note that any structure may be used an unlimited number of times, the structures may be used in any order.

Input

The first line contains a non-empty word s. The second line contains a non-empty word t. Words s and t are different. Each word consists only of lowercase English letters. Each word contains at most 100 letters.

Output

In the single line print the answer to the problem. Print "need tree" (without the quotes) if word s cannot be transformed into word teven with use of both suffix array and suffix automaton. Print "automaton" (without the quotes) if you need only the suffix automaton to solve the problem. Print "array" (without the quotes) if you need only the suffix array to solve the problem. Print "both" (without the quotes), if you need both data structures to solve the problem.

It's guaranteed that if you can solve the problem only with use of suffix array, then it is impossible to solve it only with use of suffix automaton. This is also true for suffix automaton.

Sample test(s)
input
automaton
tomat
output
automaton
input
array
arary
output
array
input
both
hot
output
both
input
need
tree
output
need tree

题意大概是说有两个字符串S和T,经过两种变换,使得两个字符串一样。一种叫"Automaton":删除任意的字符,一种叫"Array":调整字符的顺序,假如以上两种都不能满足的话输出”need tree";

测试数据就是在坑人,让你想通过测试数据来理解题意根本不可能,假如你像我是英语渣的话。

写完之后还是比较简单的。(ps:总的一个教训就是,为什么我就不能在写代码之前就有一个清楚完整的思路呢。总是漏着漏那的)

贴上自己的代码。

#include<iostream>
#include <string>
using namespace std;
int main()
{
	string str, str2;
	while (getline(cin, str))
	{
		int pos = 0, pos2 = 0;
		getline(cin, str2);
		if (str.length() == str2.length())//分为两种情况进行比较,第一种是两个字符串相等的情况,这种情况下,假如有不存在的字母的话
		{                                  //<span style="font-family: Arial, Helvetica, sans-serif;">直接输出need tree,否则就是array</span>

		<span style="white-space:pre">	</span>int len = str.length();
			int len2 = str2.length();
			for (pos = 0; pos < len; pos++)
			{
				for (pos2 = 0; pos2 < len2; pos2++)
				if (str[pos] == str2[pos2])
				{
					str2[pos2] = '0';
					break;
				}
				if (pos2 == len2)
				{
					cout << "need tree" << endl;
					break;
				}
			}
			if (pos == len)
				cout << "array" << endl;
		}
		else
		{
			int len = str.length();
			int len2 = str2.length();
			if (len2 > len)//假如下面的字符串长于要变的字符串,肯定就是need tree
				cout << "need tree" << endl;
			else{
				for (pos = 0; pos < len; pos++)
				{
					int next;
					int i;
					if (str[pos] == str2[0])
					{
						next = pos + 1;
						for (pos2 = 1; pos2 < len2; pos2++)//这边的话具体看数据理解吧 ababababa aaaaa
						{<span style="white-space:pre">			</span>//这边的话只需要顺序上是递增的即可,不需要连续,第一次是用连续去写,结果wa了
							for (i = next; i < len; i++)
							if (str[i] == str2[pos2])
							{
								next = i+1;
								break;
							}
							if (i == len)//假如检查到S的尾部都没发现与当前字母相同的字母的话,变不再检查,因为已经不满足
								break;
						}
					}
					if (pos2 == len2)//假如刚出注释位置没有执行的话,说明每个T的字母都有对应的递增序列位置的字母存在。
					{
						cout << "automaton" << endl;
						break;
					}
				}
				if (pos2 != len2)
				{
					for (pos2 = 0; pos2 < len2; pos2++)
					{
						for (pos = 0; pos < len; pos++)
						if (str[pos] == str2[pos2])
						{
							str[pos] = '0';
							break;
						}
						if (pos == len)
						{
							cout << "need tree" << endl;//此处的依据与最开始判断一样。
							break;
						}
					}
					if (len2 == pos2)
						cout << "both" << endl;
				}
			}
		}
	}
}

另外查看了别人的代码。感觉自己还是缺少经验,不能够转换看问题的思路。

比如下面代码采用的方式就是记录每个字符串出现的字母的次数。

然后去比较字符串每个字母出现的次数。

假如出现了T的字母出现次数比S的多,那么便是"need tree"

假如两个对应字母出现的次数一样多,那么说明长度一样,所含的字母也相同,只是位置不同或者连位置都相同,这时候直接输出array

假如出现了多余的字母,并且T字符串没有出现多余的字母

那就是长度上的问题还有顺序上与可能有问题。具体看代码理解吧。这种思路的话,考虑还是十分简单的。但却是非常好。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char s[105], t[105];

int sumS[27], sumT[27];
int main()
{
	int slen, tlen, i, j, sumGt, sumLt;
	scanf("%s%s", s, t);
	slen = strlen(s);
	tlen = strlen(t);
	memset(sumS, 0, sizeof(sumS));
	memset(sumT, 0, sizeof(sumT));
	for (i = 0; i<slen; ++i)
	{
		sumS[s[i] - 'a']++;//统计每个字母出现的次数
	}
	for (i = 0; i<tlen; ++i)
	{
		sumT[t[i] - 'a']++;//统计每个字母出现的次数
	}
	//sumZ = 0;
	sumGt = 0;
	sumLt = 0;
	for (i = 0; i<26; ++i)
	{
		if (sumS[i]>sumT[i]) sumGt++;
		else if (sumS[i]<sumT[i]) sumLt++;
		//else if(sumS[i]!=0) sumZ++;
	}

	if (sumLt != 0) printf("need tree\n");
	else if (sumGt == 0) printf("array\n");
	else
	{
		i = -1;
		for (j = 0; j<tlen; ++j)
		{
			while (i<slen)
			{
				++i;
				if (t[j] == s[i])
				{
					break;
				}
			}
		}
		if (i<slen) printf("automaton\n");
		else printf("both\n");
	}

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值