Chinaunix首页 | 论坛 | 博客
  • 博客访问: 41750
  • 博文数量: 23
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 137
  • 用 户 组: 普通用户
  • 注册时间: 2014-07-21 11:10
个人简介

作为一名计算机专业的白丁,我还在摸索。

文章分类

全部博文(23)

文章存档

2014年(23)

我的朋友

分类: C/C++

2014-07-23 13:35:34

Description

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 t even 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 Input

Input
automaton
tomat
Output
automaton
Input
array
arary
Output
array
Input
both
hot
Output
both
Input
need
tree
Output
need tree

Hint

In the third sample you can act like that: first transform "both" into "oth" by removing the first character using the suffix automaton and then make two swaps of the string using the suffix array and get "hot".


题意:给两个字符串s和t,automaton:删除一个字母,array:交换两个字母。判断从s变到t需要什么方法。
        只需要automaton法,输出“automaton”(不输出引号)。
         只需要array法,输出“array”。
         两个方法都需要,输出"both"。
        只用这两个方法做不到,输出“need tree”。

解题思路:设字符串s的长度为lens,字符串t的长度为lent。
        1.若lent>lens,则肯定无法通过交换和删除将s变为t,直接输出“need tree”。
        2.若lent=lens,则肯定不需要用删除法,这时只需判断是否只通过交换字母达到目的。
            定义两个数组chs[26],cht[26],分别存放字符串s和t中26个字母的个数。
            <1>若chs[i]=cht[i](i=0~25),即字符串s和字符串t中每个字母的个数相同,则可以通过交换字母将s变为t。输出“array”。
            <2>否则,输出“need tree”。
        3.若lent             <1>t中的字母s中不都有,即0~25中存在i使chs[i]             <2>t中的字母s中都有:
                (1)t中字母按顺序出现在s中,输出“automaton”。
                (2)t中字母不按顺序出现在s中,输出“both”。


代码:
#include
#include
using namespace std;
char s[101],t[101];
int main()
{
    int lens,lent,i,j;
    cin>>s>>t;
    lens=strlen(s);
    lent=strlen(t);
    if(lens     {
        cout<<"need tree"<         return 0;
    }
    else if(lens==lent)
    {
        int chs[26]={0},cht[26]={0},flag=1,ks,kt;
        for(i=0;i         {
            ks=s[i]-'a';
            kt=t[i]-'a';
            chs[ks]++;
            cht[kt]++;
        }
        for(i=0;i<26;i++)
        {
            if(chs[i]!=cht[i])
            {
                flag=0;
                break;
            }
        }
        if(flag)
            cout<<"array"<         else
            cout<<"need tree"<         return 0;
    }
    else
    {
        int chs[26]={0},cht[26]={0},ks,kt,flag=1;
        for(i=0;i         {
            ks=s[i]-'a';
            chs[ks]++;
        }
        for(i=0;i         {
            kt=t[i]-'a';
            cht[kt]++;
        }
        for(i=0;i<26;i++)
        {
            if(chs[i]             {
                flag=0;
                break;
            }
        }
        if(flag==0)
        {
            cout<<"need tree"<             return 0;
        }
        else
        {
            j=0;
            for(i=0;i             {
                if(s[i]==t[j])
                {
                    j++;
                    lent--;
                }
            }
            if(lent==0)
            {
                cout<<"automaton"<                 return 0;
            }
            else
            {
                cout<<"both"<                 return 0;
            }
        }
    }
    return 0;
}
阅读(328) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~