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甲1005 Spell it right【字符串】


下一篇:Java中创建对象的5种方式