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 ...
随机推荐
- MVC学生管理系统-阶段IV(修改学生信息)
项目源码 :https://download.csdn.net/download/weixin_44718300/11091042 前期准备,主体框架, 学生列表显示 请看阶段一文章 添加学生信息 ...
- sql 常用的语句(sql 创建表结构 修改列 清空表)
1.创建表 create Table WorkItemHyperlink ( ID bigint primary key ,--主键 WorkItemID ,) not null,--其中identi ...
- spring boot 环境配置(profile)切换
Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...
- maven工具引入lib下的jar文件
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot ...
- 关于Vue element-ui中诡异问题的解决思路
最近在做Element-ui项目时总是会出现些异步及其一些诡异问题,关于vue 的异步原理就不多说了,感觉大部分的问题都可以用Vue.nextTick来解决,Vue.nextTick是等DOM二次加载 ...
- 基本pipline用法
#!groovynode { stage('代码获取') { checkout([$class: 'SubversionSCM', additionalCredentials: [], exclude ...
- 反编译查看printf()的方法
源代码: package test2; public class ExplorationJDKSource { /** * @param args */ public static void main ...
- Runtime之方法交换
在没有一个类的实现源码的情况下,想改变其中一个方法的实现,除了继承它重写.和借助类别重名方法暴力抢先之外,还有就是方法交换 方法交换的原理:在OC中调用一个方法其实是向一个对象发送消息,查找消息的唯一 ...
- html+css web storage课上笔记 2019.3.18
存储 cookie cookie 使用文本来存储信息 使用时服务器发送cookie给客户端,下一次时,浏览器发送给服务器 web storage local storage 本地的硬件设备中,关闭后不 ...
- python刷LeetCode:5. 最长回文子串
难度等级:中等 题目描述: 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 1: 输入: "babad"输出: "bab& ...