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 ...
随机推荐
- 使用pyinotify实现加强版的linux tail -f 命令,并且对日志类型的文本进行单独优化着色显示。
tail -f命令不能自动切换切片文件,例如日志是每100M生成一个新文件,tail -f不能自动的切换文件,必须关闭然后重新运行tail -f 此篇使用pyinotify,检测文件更新,并实现tai ...
- SQLServer2008/2005 生成数据字典语句
SELECT 表名 then d.name else '' end, 表说明 then isnull(f.value,'') else '' end, 字段序号=a.colorder, 字段名=a.n ...
- SpringBoot------集成PageHelper分页功能
添加MyBatis的代码,地址 https://www.cnblogs.com/tianhengblogs/p/9537665.html 修改以下部分: 1.添加MyBatisConfig packa ...
- VSCode------搭建.net core 2.0,并配置到IIS服务器
前奏 安装VSCode最新版: https://code.visualstudio.com/ 安装window server hosting,发布和部署到IIS使用: https://www.micr ...
- SpringMVC由浅入深day01_9商品修改功能开发
9 商品修改功能开发 9.1 需求 操作流程: 1.进入商品查询列表页面 2.点击修改,进入商品修改页面,页面中显示了要修改的商品(从数据库查询) 要修改的商品从数据库查询,根据商品id(主键)查询商 ...
- 转载mysql数据库配置优化
网上有很多的文章教怎么配置MySQL服务器,但考虑到服务器硬件配置的不同,具体应用的差别,那些文章的做法只能作为初步设置参考,我们需要根据自己的情况进行配置优化,好的做法是MySQL服务器稳定运行了一 ...
- 【RF库Built-In测试】Catenate
Name:CatenateSource:BuiltIn <test library>Arguments:[ *items ]Catenates the given items togeth ...
- HttpClient(二)-- 模拟浏览器抓取网页
一.设置请求头消息 User-Agent模拟浏览器 1.当使用第一节的代码 来 访问推酷的时候,会返回给我们如下信息: 网页内容:<!DOCTYPE html> <html> ...
- error:1407742E:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert protocol version
更新的软件可以解决 TortoiseGit-preview-2.5.7.0-20180127-b2d00f8-64bit.msi和Git-2.16.2-64-bit.exe. 链接地址为: https ...
- 编写java的时候出现“编码GBK的不可映射字符”
今天在编写文件的时候,使用 javac ***.java 但是java文件里面会出现一些中文的信息,So:会报错 方法: 加参数-encoding UTF-8 例如:javac -encodig UT ...