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】【二分】的更多相关文章

  1. PAT 甲级 1077 Kuchiguse (20 分)(简单,找最大相同后缀)

    1077 Kuchiguse (20 分)   The Japanese language is notorious for its sentence ending particles. Person ...

  2. PAT甲级——1077.Kuchiguse(20分)

    The Japanese language is notorious for its sentence ending particles. Personal preference of such pa ...

  3. PAT 甲级 1077 Kuchiguse

    https://pintia.cn/problem-sets/994805342720868352/problems/994805390896644096 The Japanese language ...

  4. PAT Advanced 1077 Kuchiguse (20 分)

    The Japanese language is notorious for its sentence ending particles. Personal preference of such pa ...

  5. 1077. Kuchiguse (20)【字符串处理】——PAT (Advanced Level) Practise

    题目信息 1077. Kuchiguse (20) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B The Japanese language is notorious f ...

  6. POJ 1743 Musical Theme (字符串HASH+二分)

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15900   Accepted: 5494 De ...

  7. 洛谷P1117 优秀的拆分【Hash】【字符串】【二分】【好难不会】

    题目描述 如果一个字符串可以被拆分为AABBAABB的形式,其中 A和 B是任意非空字符串,则我们称该字符串的这种拆分是优秀的. 例如,对于字符串aabaabaaaabaabaa,如果令 A=aabA ...

  8. PAT 1077 Kuchiguse [一般]

    1077 Kuchiguse (20 分) The Japanese language is notorious for its sentence ending particles. Personal ...

  9. PAT 1077 Kuchiguse

    1077 Kuchiguse (20 分)   The Japanese language is notorious for its sentence ending particles. Person ...

随机推荐

  1. Disconf (version : 2.6.21)

    通常我们会做如下配置:(disconf 2.6.21) <!-- 一次扫描 --> <bean id="disconfMgrBean" class="c ...

  2. Linux环境SVN命令行使用经验总结(转)

    在windows机器上开发得差不多了之后,打包传送到开发机编译,在开发机上解决编译错误. [缺点] 浪费时间在打包解包,机器间传输代码. 在windows机器上开发之后,check in代码进分支,在 ...

  3. Git Step by Step – (8) Git的merge和rebase

    前面一篇文章中提到了"git pull"等价于"git fetch"加上"git merge",然后还提到了pull命令支持rebase模式 ...

  4. Unity Shader 修改自定义变量的值

    Properties { _R(,)) = 1.0 _ColorTex("ColorTex (RGB)", 2D) = "red" {} } SubShader ...

  5. [Python] 正确复制列表的方法

    new = old[:] Python老鸟都知道以上代码是什么意思.它复制列表old到new.它对于新手来说是种困惑而且应该避免使用这种方法.不幸的是[:]标记法被广泛使用,可能是Python程序员不 ...

  6. 使用node中的iconv-lite实现对“gbk”格式的转码

    在window中,gbk和utf-8是最常见的两种格式,但是我们在显示的时候往往需要将GBK转换为UTF-8,我现在有一个同步读取文件的操作: const fs = require('fs'); co ...

  7. MFC框架程序解析

    MFC的 程序框架: WinMain函数:程序首先到达全局变量theApp,再到达theAPP的构造函数,最后到达WinMain函数处. 问:为何要定义一个全局对象theAPP,让其在WinMain函 ...

  8. 【Linux】python 2.x 升级 python3.x 之后 yum命令出现except OSError, e: ^ SyntaxError: invalid syntax

    python2.7升级到python3.6.4 文章链接 :  https://zhuanlan.zhihu.com/p/33660059 我在服务器上.把linux默认安装的python2.7 升级 ...

  9. [转载]Array.prototype.slice.call(arguments,1)原理

    Array.prototype.slice.call(arguments,1)该语句涉及两个知识点. arguments是一个关键字,代表当前参数,在javascript中虽然arguments表面上 ...

  10. 【数据库系列】MySql中的select的锁表范围

    由于InnoDB预设的是Row-Level Lock,只有明确指定主键的时候MySql才会执行Row lock,否则MySql将会执行Table Lock. 1.明确指定主键则是行锁 2.明确指定主键 ...