//暴力,从每一行的开始处开始寻找要查询的字符
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char str[100005];
int main(){
while(gets(str)){
for(int i=0; str[i]; ++i)
if(str[i]=='A'){
if(strstr(str+i, "Apple") == str+i)
printf("MAI MAI MAI!\n");
}
else if(str[i]=='i'){
if(strstr(str+i, "iPhone") == str+i || strstr(str+i, "iPod") == str+i || strstr(str+i, "iPad") == str+i)
printf("MAI MAI MAI!\n");
}
else if(str[i]=='S')
if(strstr(str+i, "Sony") == str+i)
printf("SONY DAFA IS GOOD!\n");
}
return 0;
}
//将要匹配的字符串(也就是题目中查询文本中出现的5个单词)建立trie树,然后生成AC_自动机.....
#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
#include<algorithm>
using namespace std;
int trie[30][200];
int vis[30], fail[6000];//vis标记的是单词的末尾字符所在的节点
char str[][10] = {"Apple", "iPhone", "iPod", "iPad", "Sony"};
int cnt;
void buildT(){
for(int i=0; i<5; ++i){
int u=0;
for(int j=0; str[i][j]; ++j){
if(trie[u][str[i][j]] == 0)
trie[u][str[i][j]] = ++cnt;
u=trie[u][str[i][j]];
}
vis[u]=1;
}
}
void getFail(){
queue<int>q;
for(int i=0; i<200; ++i)
if(trie[0][i]) q.push(trie[0][i]);
while(!q.empty()){
int u = q.front();
q.pop();
int v;
for(int i=0; i<200; ++i)
if(v = trie[u][i]){
fail[v] = trie[fail[u]][i];
q.push(v);
}
else
trie[u][i] = trie[fail[u]][i];
}
}
void getText(char *ch){
int u=0;
for(int i=0; ch[i]; ++i){
int v = trie[u][ch[i]];
u=v;
while(v){
if(vis[v] && (ch[i]=='d' || ch[i]=='e'))
printf("MAI MAI MAI!\n");
else if(vis[v] && ch[i]=='y')
printf("SONY DAFA IS GOOD!\n");
v = fail[v];
}
}
}
char text[10000];
int main(){
buildT();
getFail();
while(gets(text)){
getText(text);
}
return 0;
}