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 ...
随机推荐
- Python 常用类库
python除了关键字(keywords)和内置的类型和函数(builtins),更多的功能是通过libraries(即modules)来提供的. 常用的libraries(modules)如下: 1 ...
- task.factory.startnew()
1.委托: public delegate int Math(int param1,int param2);定义委托类型 Public int Add(int param1,int param2)// ...
- Tomcat------如何打开配置界面
如图操作即可:
- Java利用for循环输出空心的菱形
编写程序,在控制台上输出空心菱形,对角距离为6. public class Diamond { public static void main(String[] args) { printHollow ...
- Go之获取Windows下文件是否隐藏
起初,做了个小程序,用来检测磁盘中所有的文件 package main import( "fmt" "io/ioutil" "os" ) v ...
- 胡思乱想 & 胡言乱语
其大无外,其小无内,在数学上是不存在的,有无穷大,又有无限逼近于0而永远不等于0 现实中,人们对事物的认知局限于科学工艺的发展,往小的方面说,在没有显微镜之前,我们能看到的最小的东西莫过于尘埃,其后认 ...
- 【代码审计】ThinkSNS_V4 任意文件下载漏洞分析
0x00 环境准备 ThinkSNS官网:http://www.thinksns.com 网站源码版本:ThinkSNS V4 更新时间:2017-09-13 程序源码下载:http://www ...
- Flash XSS 漏洞实例
www.bsdxm.com/zeroclipboard/ZeroClipboard.swf?id=\"))}catch(e){alert(/xss/);}//&width=500&a ...
- 使用dshow捕获摄像头图像
#include "stdafx.h" #include <DShow.h> #include <Guiddef.h> #include <strmi ...
- MongoDB(六)-- 集群搭建
一.集群介绍 sharding通过将数据集分布于多个也称作分片(shard)的节点上来降低单节点的访问压力.每个分片都是一个独立的数据库,所有的分片组合起来构成一个逻辑上的完整意义的数据库.因此,分片 ...