PAT甲1077 Kuchiguse【字符串】【暴力】【Hash】【二分】
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, write nai
.
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
题意:
给n个串求所有串的最长公共后缀。
思路:
觉得自己就是一个傻逼....
看到题目想到的直接是Hash+二分,敲完了WA了去看别人写的发现直接暴力两两找就行了,因为LCS长度是非递增的。
交完了暴力突然明白HashWA了是因为题目的意思是区分大小写,我以为那句话的意思是不区分大小写,还把大写转成了小写。
想太多。
#include <iostream>
#include <set>
#include <cmath>
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
using namespace std;
typedef long long LL;
#define inf 0x7f7f7f7f const int maxn = ;
int n;
char s1[], s2[]; int main()
{
scanf("%d", &n);
getchar();
scanf("%[^\n]", s1);
int len = inf;
for(int i = ; i < n; i++){
getchar();
scanf("%[^\n]", s2);
int k1 = strlen(s1) - , k2 = strlen(s2) - ;
if(k1 > k2){
swap(s1, s2);
swap(k1, k2);
}
while(k1 >= && s1[k1] == s2[k2]){
k1--;k2--;
}
//cout<<k1<<endl;
if(strlen(s1) - k1 - < len){
len = strlen(s1) - k1 - ;
}
//len = min(len, strlen(s1) - k1);
}
//cout<<len<<endl;
if(!len){
printf("nai\n");
}
else{
int l = strlen(s1) - ;
for(int i = l - len + ; i <= l; i++){
printf("%c", s1[i]);
}
printf("\n");
}
return ;
}
Hash + 二分
#include <iostream>
#include <set>
#include <cmath>
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
using namespace std;
typedef long long LL;
#define inf 0x7f7f7f7f const int maxn = ;
int n, len[maxn];
char s[maxn][];
unsigned long long h[maxn][], p[]; unsigned long long get_hash(int i, int j, int id)
{
return h[id][j] - h[id][i - ] * p[j - i + ];
} bool check(int mid)
{
unsigned long long x = get_hash(len[] - mid + , len[], );
//cout<<x<<endl;
for(int i = ; i <= n; i++){
//cout<<get_hash(len[i] - mid + 1, len[i], i)<<endl;
if(x != get_hash(len[i] - mid + , len[i], i)){
return false;
}
}
return true;
} int main()
{
scanf("%d", &n);
p[] = ;
for(int i = ; i < ; i++){
p[i] = p[i - ] * ;
}
int minlen = inf;
for(int i = ; i <= n; i++){
getchar();
scanf("%[^\n]", s[i] + );
//printf("%s", s[i] + 1);
len[i] = strlen(s[i] + );
minlen = min(minlen, len[i]);
h[i][] = ;
for(int j = ; j <= len[i]; j++){
/*if(s[i][j] >= 'A' && s[i][j] <= 'Z'){
s[i][j] = s[i][j] - 'A' + 'a';
}*/
h[i][j] = h[i][j - ] * + (int)s[i][j];
}
} //cout<<minlen<<endl;
/*for(int i = 1; i <= n; i++){
cout<<len[i]<<endl;
}*/
int st = , ed = minlen, ans = -;
while(st <= ed){
int mid = (st + ed) / ;
//cout<<mid<<endl;
if(check(mid)){
st = mid + ;
ans = mid;
}
else{
ed = mid - ;
}
} //cout<<ans<<endl;
if(ans < ){
printf("nai\n");
}
else{
bool flag = true;
for(int j = len[] - ans + ; j <= len[]; j++){
if(flag && s[][j] == ' '){
continue;
}
if(flag && s[][j] != ' '){
flag = false;
}
printf("%c", s[][j]);
}
printf("\n");
} return ;
}
PAT甲1077 Kuchiguse【字符串】【暴力】【Hash】【二分】的更多相关文章
- PAT 甲级 1077 Kuchiguse (20 分)(简单,找最大相同后缀)
1077 Kuchiguse (20 分) The Japanese language is notorious for its sentence ending particles. Person ...
- PAT甲级——1077.Kuchiguse(20分)
The Japanese language is notorious for its sentence ending particles. Personal preference of such pa ...
- PAT 甲级 1077 Kuchiguse
https://pintia.cn/problem-sets/994805342720868352/problems/994805390896644096 The Japanese language ...
- PAT Advanced 1077 Kuchiguse (20 分)
The Japanese language is notorious for its sentence ending particles. Personal preference of such pa ...
- 1077. Kuchiguse (20)【字符串处理】——PAT (Advanced Level) Practise
题目信息 1077. Kuchiguse (20) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B The Japanese language is notorious f ...
- POJ 1743 Musical Theme (字符串HASH+二分)
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15900 Accepted: 5494 De ...
- 洛谷P1117 优秀的拆分【Hash】【字符串】【二分】【好难不会】
题目描述 如果一个字符串可以被拆分为AABBAABB的形式,其中 A和 B是任意非空字符串,则我们称该字符串的这种拆分是优秀的. 例如,对于字符串aabaabaaaabaabaa,如果令 A=aabA ...
- PAT 1077 Kuchiguse [一般]
1077 Kuchiguse (20 分) The Japanese language is notorious for its sentence ending particles. Personal ...
- PAT 1077 Kuchiguse
1077 Kuchiguse (20 分) The Japanese language is notorious for its sentence ending particles. Person ...
随机推荐
- [RN] 03 - Resource Collection & AWS Auth
那些资源 一.三个例子 iReading Bilibili-React-Native ZhiHuDaily-React-Native 二.IM 例子 1. React Native Socket.io ...
- Android图片处理(Matrix,ColorMatrix) - 转载
Android图片处理(Matrix,ColorMatrix) 转载自:http://www.cnblogs.com/leon19870907/articles/1978065.html 在编程中有时 ...
- SpringMVC -- 梗概--源码--壹--收参
附:实体类 Class : User package com.c61.entity; import java.text.SimpleDateFormat; import java.util.Date; ...
- Linux 日常运维
查看用户信息:w 查看系统负载:uptime 查看系统资源使用情况:vmstat 查看进程动态:top 查看网卡流量:sar 查看网卡流量:nload 查看磁盘读写:iostat 查看磁盘读写:iot ...
- es 5.0的下载安装for mac
为了学习es的同学少走些弯路,特此记下笔记,以供学习. 我装的es是5.4.3 一,下载安装es, 1,es 5.0之后变化很大,对jdk要求为1.8,(先升级jdk) 2,下载地址 :https:/ ...
- SFTP文件下载
FTP并不是唯一的上传文件的方法,大部分情况下都可使用sftp代替.sftp是什么呢? sftp是Secure File Transfer Protocol的缩写,安全文件传送协议.可以为传输文件提供 ...
- 【laravel5.6】 Illuminate\Database\QueryException : SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes
在进行数据迁移时候报错: 特殊字段太长报错, php artisan migrate 现在utf8mb4包括存储emojis支持.如果你运行MySQL v5.7.7或者更高版本,则不需要做任何事情. ...
- 【大数据系列】apache hive 官方文档翻译
GettingStarted 开始 Created by Confluence Administrator, last modified by Lefty Leverenz on Jun 15, 20 ...
- Win7 系统如何关闭休眠功能?(已解决)
一不小心,使用了系统的 休眠 功能. 一开始也没注意. 后来,发现C盘(系统盘)怎么变小了? 一想,应该是休眠的问题. 我就想把它生成的文件给删了. 为此,我特意把 文件夹选项 里的 显示隐藏文件和文 ...
- Android NDK学习(4)使用cygwin生成.so库文件
转:http://www.cnblogs.com/fww330666557/archive/2012/12/14/2817389.html 简单的示例: makefile文件: LOCAL_PATH: ...