pat 1077 Kuchiguse(20 分) (字典树)
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 分) (字典树)的更多相关文章
- PAT 甲级 1077 Kuchiguse (20 分)(简单,找最大相同后缀)
1077 Kuchiguse (20 分) The Japanese language is notorious for its sentence ending particles. Person ...
- PAT Advanced 1077 Kuchiguse (20 分)
The Japanese language is notorious for its sentence ending particles. Personal preference of such pa ...
- PAT甲级——1077.Kuchiguse(20分)
The Japanese language is notorious for its sentence ending particles. Personal preference of such pa ...
- 【PAT甲级】1077 Kuchiguse (20 分)(cin.ignore()吃掉输入n以后的回车接着用getine(cin,s[i])输入N行字符串)
题意: 输入一个正整数N(<=100),接着输入N行字符串.输出N行字符串的最长公共后缀,否则输出nai. AAAAAccepted code: #include<bits/stdc++. ...
- PAT 1077 Kuchiguse
1077 Kuchiguse (20 分) The Japanese language is notorious for its sentence ending particles. Person ...
- 1077. Kuchiguse (20)【字符串处理】——PAT (Advanced Level) Practise
题目信息 1077. Kuchiguse (20) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B The Japanese language is notorious f ...
- PAT 1077 Kuchiguse [一般]
1077 Kuchiguse (20 分) The Japanese language is notorious for its sentence ending particles. Personal ...
- PAT (Advanced Level) 1077. Kuchiguse (20)
最长公共后缀.暴力. #include<cstdio> #include<cstring> #include<cmath> #include<vector&g ...
- PAT甲题题解-1077. Kuchiguse (20)-找相同后缀
#include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...
随机推荐
- Vue 上传材料并传给后端接口(使用input)
最近工作中接到一个需求,需要上传一个文件材料,提交时传给后端.使用的框架是Vue,废话不说直接上代码 <template> <div> <input type=" ...
- date命令查看与修改
在我们使用linux服务器时,肯定会遇到Linux服务器时间不准确的情况如何查看Linux系统的时间,如何修改Linux系统上的当前时间呢. 查看Linux系统当前时间: 命令: date +回车 修 ...
- Open vSwitch系列之一 Open vSwitch诞生
Open vSwitch系列之一 Open vSwitch诞生 Open vSwitch系列之二 安装指定版本ovs 2006年,SDN诞生于美国GENI项目资助的斯坦福大学Clean Slate课题 ...
- Redis(十一)缓存设计
一.缓存的收益和成本 左侧为客户端直接调用存储层的架构,右侧为比较典型的缓存层+存储层架构, 缓存加入后带来的收益如下: 加速读写:因为缓存通常都是全内存的(例如Redis.Memcache),而存储 ...
- 获取gps信息
(一)plist修改 添加如下变量 (二)新建视图用来启动Gps 此视图控制器继承CLLocationManagerDelegate #import <UIKit/UIKit.h> #im ...
- 为什么磁盘慢会导致Linux负载飙升?
一.CPU利用率和负载率的区别 这里要区别CPU负载和CPU利用率,它们是不同的两个概念,但它们的信息可以在同一个top命令中进行显示.CPU利用率显示的是程序在运行期间实时占用的CPU百分比,这是对 ...
- Bash 通配符、正则表达式、扩展正则表达式
BASH中的通配符(wildcard) *:任意长度的任意字符. ?:任意单个字符 []:匹配范围 [^]:排除匹配范围 [:alnum:]:所有字母和数字 [:alpha:]:所有字母 [:digi ...
- 学习笔记32_EF查询优化
*如果有 var temp = from m in dbContext.Model1 where m.属性1 == value select m; foreach(var m1 in temp)//这 ...
- 一道国外前端面试题引发的Coding...
刚刚看到CSDN微信公众号一篇文章,关于国外程序员面试前端遇到的一道测试题,有点意思,遂写了下代码,并记录一下~ 题目是这样的: ['Tokyo', 'London', 'Rome', 'Donlon ...
- 爬虫之scrapy安装与基本使用
安装:pip install scrapy基本使用 1. 创建项目:scrapy startproject 项目名 cd 项目名 2. 生成爬虫 1. 创建spider爬虫:scrapy genspi ...