PAT甲级——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
题目要求是求相同的字符串后缀
一开始的解题思路是:
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
int N;
int ans=0;
scanf("%d",&N);
int minlen = 256;
char a[101][300];
getchar();
for(int i=0;i<N;i++)
{
fgets(a[i],256,stdin);
int len = strlen(a[i]);
if(minlen>len) minlen =len;
for(int j=0;j<minlen/2;j++)//reverse
{
char temp;
temp = a[i][j];
a[i][j]=a[i][-j+len-1];
a[i][-j+len-1]=temp;
}
}
for(int i=1;i<minlen;i++)
{
bool flag=true;
char c = a[0][i];
for(int j = 1;j<N;j++)
{
if(c!=a[j][i])
{
flag = false;
break;
}
}
if(flag) ans++;
else break;
}
if(ans)
{
for(int i=ans-1;i>=1;i--)
{
printf("%c",a[0][i]);
}
}
else
{
printf("nai");
}
return 0;
}
感觉有点混乱
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n;
scanf("%d\n", &n);
string ans;
for(int i = 0; i < n; i++) {
string s;
getline(cin, s);
int lens = s.length();
reverse(s.begin(), s.end());
if(i == 0) {
ans = s;
continue;
}
else {
int lenans = ans.length();
int minlen = min(lens, lenans);
for(int j = 0; j < minlen; j++) {
if(ans[j] != s[j]) {
ans = ans.substr(0, j);
break;
}
}
}
}
reverse(ans.begin(), ans.end());
if (ans.length() == 0)
ans = "nai";
cout << ans;
return 0;
}
以上的解法更加简便
使用到的新鲜函数是:getline, reverse, min
1. getline
istream& getline (char* s, streamsize n );
istream& getline (char* s, streamsize n, char delim );
字符串的输入方式之一,特点是遇到空格不停止输入
2. reverse
反转范围中元素的顺序[first,last)
3. min
输出两数中较小的一个,同理max是较大那个
PAT甲级——1077.Kuchiguse(20分)的更多相关文章
- PAT 甲级 1077 Kuchiguse (20 分)(简单,找最大相同后缀)
1077 Kuchiguse (20 分) The Japanese language is notorious for its sentence ending particles. Person ...
- PAT Advanced 1077 Kuchiguse (20 分)
The Japanese language is notorious for its sentence ending particles. Personal preference of such pa ...
- PAT 甲级 1035 Password (20 分)(简单题)
1035 Password (20 分) To prepare for PAT, the judge sometimes has to generate random passwords for ...
- PAT 甲级 1061 Dating (20 分)(位置也要相同,题目看不懂)
1061 Dating (20 分) Sherlock Holmes received a note with some strange strings: Let's date! 3485djDk ...
- 【PAT甲级】1077 Kuchiguse (20 分)(cin.ignore()吃掉输入n以后的回车接着用getine(cin,s[i])输入N行字符串)
题意: 输入一个正整数N(<=100),接着输入N行字符串.输出N行字符串的最长公共后缀,否则输出nai. AAAAAccepted code: #include<bits/stdc++. ...
- PAT甲级——1035 Password (20分)
To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem ...
- PAT 甲级 1077 Kuchiguse
https://pintia.cn/problem-sets/994805342720868352/problems/994805390896644096 The Japanese language ...
- PAT甲级——1061 Dating (20分)
Sherlock Holmes received a note with some strange strings: Let's date! 3485djDkxh4hhGE 2984akDfkkkkg ...
- PAT甲级——1005.SpellItRight(20分)
Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output e ...
随机推荐
- oracle查询语句注意事项:
我想查出datatype 不等于1的所有结果,包括空. '; //这条sql查不出datatype为空的数据 发现oracle需要使用 is null .is not null查询空或非空 ' ...
- NCRE Java二级备考方案
一 配合大纲梳理基本知识点 二 多在233网校刷题
- 基于Docker本地运行Kubernetes
基于Docker本地运行Kubernetes 概览 下面的指引将高速你如何通过Docker创建一个单机.单节点的Kubernetes集群. 下图是最终的结果: 先决条件 \1. 你必须拥有一台安装有D ...
- 2020/2/6 PHP编程学习
今天把后台数据库处理好了,用了框架后真就是搬砖的一天..晚上继续刷题,明天把数据库处理完,这样一个商城框架就有了:
- [GXYCTF2019]Ping Ping Ping
0x00 知识点 命令执行变量拼接 /?ip=127.0.0.1;a=g;cat$IFS$1fla$a.php 过滤bash用sh执行 echo$IFS$1Y2F0IGZsYWcucGhw|base6 ...
- eclipse中tomcat添加或移除web项目出错,显示无资源能被添加或移除
错误截图 之前一直都能正常使用,今天莫名其妙出现这个错误 解决办法 https://blog.csdn.net/u012956987/article/details/79134474 右击项目,在属性 ...
- c++ 排序 冒泡 插入 选择 快速
//冒泡 #include <iostream> using namespace std; void bubbleSort(int* list,int index) { ;i--) //i ...
- 经典线段树 UVALive 3938/UVA 1400
题意:就是相当于动规里面的求最大连续子串,不同的是,这里需要读入一个区间x,y,输出的区间 a,b 且x<=a<=b<=y,使得a b的连续子串最长,而且询问次数达到了10的五次方. ...
- 吴裕雄--天生自然MySQL学习笔记:MySQL 数据类型
MySQL中定义数据字段的类型对你数据库的优化是非常重要的. MySQL支持多种类型,大致可以分为三类:数值.日期/时间和字符串(字符)类型. 数值类型 MySQL支持所有标准SQL数值数据类型. 这 ...
- 安卓和iOS统一下载页面
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...