1077 Kuchiguse(20 分)

The Japanese language is notorious for its sentence ending particles. Personal preference of such particles can be considered as a reflection of the speaker's personality. Such a preference is called "Kuchiguse" and is often exaggerated artistically in Anime and Manga. For example, the artificial sentence ending particle "nyan~" is often used as a stereotype for characters with a cat-like personality:

  • Itai nyan~ (It hurts, nyan~)

  • Ninjin wa iyada nyan~ (I hate carrots, nyan~)

Now given a few lines spoken by the same character, can you find her Kuchiguse?

Input Specification:

Each input file contains one test case. For each case, the first line is an integer N (2≤N≤100). Following are N file lines of 0~256 (inclusive) characters in length, each representing a character's spoken line. The spoken lines are case sensitive.

Output Specification:

For each test case, print in one line the kuchiguse of the character, i.e., the longest common suffix of all N lines. If there is no such suffix, writenai.

Sample Input 1:

3
Itai nyan~
Ninjin wa iyadanyan~
uhhh nyan~

Sample Output 1:

nyan~

Sample Input 2:

3
Itai!
Ninjinnwaiyada T_T
T_T

Sample Output 2:

nai
 #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <queue>
#include <set>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int MAX = 1e5 + ; int n, rt = , str_len = INF;
struct node
{
int id, cnt, next[];
} P[MAX];
char shortest_str[];
stack <char> st; void init(int x)
{
for (int i = ; i <= ; ++ i)
P[x].next[i] = -;
} void my_insert(char *S, int len, int x)
{
int now = ;
for (int i = ; i < len; ++ i)
{
if (P[now].next[S[i]] == -)
{
P[now].next[S[i]] = ++ rt;
init(rt);
}
now = P[now].next[S[i]];
P[now].cnt ++;
}
} int my_find(int len)
{
int now = ;
for (int i = ; i < len; ++ i)
{
if (P[now].next[shortest_str[i]] == -) return -;
now = P[now].next[shortest_str[i]];
}
return P[now].cnt;
} int main()
{
// freopen("Date1.txt", "r", stdin);
scanf("%d", &n);
init();
getchar();
for (int i = ; i < n; ++ i)
{
char buf[];
string temp;
getline(cin, temp);
int len = temp.size();
for (int j = , k = len - ; k >= ; -- k, ++ j)
buf[j] = temp[k];
buf[len] = '\0';
my_insert(buf, len, i);
if (len < str_len)
{
str_len = len;
strcpy(shortest_str, buf);
}
} if (P[P[].next[shortest_str[]]].cnt != n)
{
printf("nai\n");
return ;
}
for (int i = str_len; i >= ; -- i)
{
if (my_find(i) == n)
{
for (int j = ; j < i; ++ j)
st.push(shortest_str[j]);
break;
}
}
while (st.size())
{
printf("%c", st.top());
st.pop();
} return ;
}

pat 1077 Kuchiguse(20 分) (字典树)的更多相关文章

  1. PAT 甲级 1077 Kuchiguse (20 分)(简单,找最大相同后缀)

    1077 Kuchiguse (20 分)   The Japanese language is notorious for its sentence ending particles. Person ...

  2. PAT Advanced 1077 Kuchiguse (20 分)

    The Japanese language is notorious for its sentence ending particles. Personal preference of such pa ...

  3. PAT甲级——1077.Kuchiguse(20分)

    The Japanese language is notorious for its sentence ending particles. Personal preference of such pa ...

  4. 【PAT甲级】1077 Kuchiguse (20 分)(cin.ignore()吃掉输入n以后的回车接着用getine(cin,s[i])输入N行字符串)

    题意: 输入一个正整数N(<=100),接着输入N行字符串.输出N行字符串的最长公共后缀,否则输出nai. AAAAAccepted code: #include<bits/stdc++. ...

  5. PAT 1077 Kuchiguse

    1077 Kuchiguse (20 分)   The Japanese language is notorious for its sentence ending particles. Person ...

  6. 1077. Kuchiguse (20)【字符串处理】——PAT (Advanced Level) Practise

    题目信息 1077. Kuchiguse (20) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B The Japanese language is notorious f ...

  7. PAT 1077 Kuchiguse [一般]

    1077 Kuchiguse (20 分) The Japanese language is notorious for its sentence ending particles. Personal ...

  8. PAT (Advanced Level) 1077. Kuchiguse (20)

    最长公共后缀.暴力. #include<cstdio> #include<cstring> #include<cmath> #include<vector&g ...

  9. PAT甲题题解-1077. Kuchiguse (20)-找相同后缀

    #include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...

随机推荐

  1. PMBOK(第六版) PMP笔记——《十一》第十一章(项目风险管理)

    PMBOK(第六版) PMP笔记——<十一>第十一章(项目风险管理) 第十一章 风险管理: 项目的独特性导致项目充满风险,项目风险是一种不确定的事件或条件,可能发生.将 要发生,也可能不发 ...

  2. luoguP1006 传纸条

    题目描述 Description 小渊和小轩是好朋友也是同班同学,他们在一起总有谈不完的话题.一次素质拓展活动中,班上同学安排做成一个 m" role="presentation& ...

  3. [JOYOI1510] 专家复仇 - Floyd

    题目限制 时间限制 内存限制 评测方式 题目来源 1000ms 131072KiB 标准比较器 Local 题目背景 外星人完成对S国的考察后,准备返回,可他们的飞碟已经没燃料了……S国的专家暗自窃喜 ...

  4. linux::jsoncpp库

    下载库:http://sourceforge.net/projects/jsoncpp/files/ tar -zxvf jsoncpp-src- -C jsoncpp () 安装 scons $ s ...

  5. php函数分为哪两种?

    PHP的真正威力源自于它的函数.函数分为内置函数和自定义函数. 内置函数 所谓PHP内置函数,就是在php程序的库里面已经定义了的函数,比如echo,mysql_connect,include_onc ...

  6. 06_基本框架_VMCS_GuestArea

    1 Guest-State Area 1.1 References 注释: 其实 内容很多 一般是不背 的:需要的时候来查就行:但是要大致知道是做啥的. 代码 > 设置 Guest Area / ...

  7. C# 闭包对像

    主要内容: 1.描述出现的现像 2.分析其出现的原因 3.提示 一.看如下一段代码及结果 class Program { static void Main(string[] args) { List& ...

  8. 非确定的自动机NFA确定化为DFA

    摘要: 在编译系统中,词法分析阶段是整个编译系统的基础.对于单词的识别,有限自动机FA是一种十分有效的工具.有限自动机由其映射f是否为单值而分为确定的有限自动机DFA和非确定的有限自动机NFA.在非确 ...

  9. mysql库表优化实例

    一.SQL优化 1.优化SQL一般步骤 1.1 查看SQL执行频率 SHOW STATUS LIKE 'Com_%'; Com_select:执行SELECT操作的次数,一次查询累加1.其他类似 以下 ...

  10. fenby C语言 P23

    #include <stdio.h> int main(){ int i,max,a[5]={10,5,20,31,4}; max=a[0]; for(i=0;i<5;i++) if ...