[The Preliminary Contest for ICPC Asia Xuzhou 2019 - 徐州网络赛D] Carneginon

Carneginon

Carneginon was a chic bard. But when he was young, he was frivolous and had joined many gangs. Recently, Caneginon was to be crowned, because the king was shocked by his poems and decided to award him the gold medal lecturer. Therefore, Most of people in the Kingdom came to visit him.

However, as a medal lectirer, Carneginon must treat the visitors kindly, including elders and younger generations. In order to maintain order, every visitor received a license with a magic field engraved on it. And the magic field on the licence was made up of lowercase letters.

Carneginon had a unique licence, which could judge whether others are his older or younger. Now, we assume that the sequence on Carneginon's licence is TT and the sequence on visitors' licence is SS. For each visitor,

  • If the length of TT is longer than the length of SS, it's obviously that the visitor is younger. And if SS is a substring of TT, Carneginon would call the visitor my child!. Otherwise, Carneginon would call the visitor oh, child!.

  • If the length of TT is less than the length of SS, it's obviously that the visitor is elder. And if TT is a substring of SS, Carneginon would call the visitor my teacher!. Otherwise, Carneginon would call the visitor senior!.

  • Of course, if the length of TT is equal to the length of SS, the visitor is Carneginon's peer. And if TT is equal to SS, it shows that the visitor entered through an improper way and Carneginon would shout jntm!. Otherwise, Carneginon would call the visitor friend!.

Now, you know the TT (Carneginon's licence), qq (the number of visitors) and each visitor's licence(S_iSi​). Can you judge what Caneginon needs to say when he sees every visitor?

Input

The first line is a string TT, representing Carneginon's license.

The second line is and integer qq, which means the number of visitors.

Then mm lines, for each line, there is a string SS, denoting the visitor's license.

1 \leq |T| \leq 10^51≤∣T∣≤105, 1 \leq |S| \leq 10^51≤∣S∣≤105, 1 \leq q \leq 10001≤q≤1000

It is guaranteed that q \times (|S|+|T|) \leq 10^7q×(∣S∣+∣T∣)≤107.

Output

There are qq lines.

For each SS, output what Carneginon should say correctly.

样例输入

abcde
6
abcde
aaaaa
abcd
aaaa
abcdef
abccdefg

样例输出

jntm!
friend!
my child!
oh, child!
my teacher!
senior!

 KMP模板题。

#include <bits/stdc++.h>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 1e5+5;
char T[maxn], S[maxn];
int nxt[maxn];
int q;

void read()
{
    scanf("%s", T);
    scanf("%d", &q);
}

void getnxt(char* p)
{
    int len = strlen(p);
    int k = -1, j = 0;
    nxt[0] = -1;
    while(j < len)
    {
        if(k == -1 || p[k] == p[j])
        {
            k++;
            j++;
            if(p[k] != p[j])
                nxt[j] = k;
            else
                nxt[j] = nxt[k];
        }
        else
            k = nxt[k];
    }
}

bool KMP(char* s, char* p)
{
    getnxt(p);
    int slen = strlen(s), plen = strlen(p);
    int i = 0, j = 0;
    while(i < slen && j < plen)
    {
        if(j == -1 || s[i] == p[j])
        {
            i++;
            j++;
        }
        else
            j = nxt[j];
    }
    if(j == plen)
        return 1;
    return 0;
}

void solve()
{
    while(q--)
    {
        scanf("%s", S);
        if(strlen(T) > strlen(S))
        {
            if(KMP(T, S))
                printf("my child!\n");
            else
                printf("oh, child!\n");
        }
        else if(strlen(T) < strlen(S))
        {
            if(KMP(S, T))
                printf("my teacher!\n");
            else
                printf("senior!\n");
        }
        else if(strcmp(T, S))
            printf("friend!\n");
        else
            printf("jntm!\n");
    }
}

int main()
{
    read();
    solve();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/HNUCSEE_LJK/article/details/100638179