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. The Mininum Function Value (luoguP2085 最小函数值)

    Background\text{Background}Background 1. CSDN's been upgraded today. There's one MORE ad for each pa ...

  2. python编程系列---global的使用注意点

    global用法虽然很简单,当在函数中要修改全局变量时要使用到,但也要注意一个小问题,看下面的代码: 调用append()方法,只是修改,并不是赋值操作,不需要global提前声明 temp_num ...

  3. day06整理

    一.上节课回顾 (一)什么是文件 操作系统提供的虚拟单位,用来存储信息 (二)文件打开的步骤 找到文件的路径 file_path 打开文件open() 读取/修改文件f.read()/f.write( ...

  4. 基于redis解决session分布式一致性问题

    1.session是什么 当用户在前端发起请求时,服务器会为当前用户建立一个session,服务器将sessionId回写给客户端,只要用户浏览器不关闭,再次请求服务器时,将sessionId传给服务 ...

  5. OptimalSolution(5)--数组和矩阵问题(1)简单

    一.转圈打印矩阵 题目:给定一个整型矩阵matrix,按照转圈的方式打印它. 要求:额外空间复杂度为O(1) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 打印结果为: ...

  6. Andriod Studio设置类默认签名模板

  7. django-数据库之连接数据库

    1.连接数据库出现的一些问题 基本目录如下: 首先我们pip install pymysql 然后在项目中,进行配置settings.py: 然后在__init__.py中进行输入: 启动服务器: 报 ...

  8. fenby C语言 P11

    else {} if {} #include int main() { int a=15; if(a%2==0) { printf("我是偶数!"); }else { printf ...

  9. SpringCloud之Feign负载均衡(四)

    整合Feign pom.xml <dependency> <groupId>org.springframework.cloud</groupId> <arti ...

  10. 如何使用git上传代码

    首先在github 上创建好远程仓库,并拷贝仓库地址,接下来按照以下步骤:   1.打开命令行进入要上传的文件目录,初始化 git init  2. 建立远程仓库,git remote add ori ...