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 ...
随机推荐
- Vue中 几个常用的命名规范
1,组件名 官方推荐的组件名是 每个单词首字母大写(PascalCase) 或者 全小写用 - 连接(kebab-case) . 在DOM中使用的时候, 改为全小写, 单词之间用 - 连接. Vue. ...
- 【STM32H7教程】第50章 STM32H7的LCD控制器LTDC基础知识和HAL库API
完整教程下载地址:http://www.armbbs.cn/forum.php?mod=viewthread&tid=86980 第50章 STM32H7的LCD控制器LTDC基础 ...
- Ajax校验用户名是否可用
准备 导包:DBUtil,JDBC,C3P0 在src下导入c3p0-config.xml 导入JDBCUtil 创建数据库 第2.3.条查看https://blog.csdn.net/weixin_ ...
- jsp页面引入不了js路径没错
最近搞开发,发现有个jsp页面引入不了js:很是神奇,路径什么的都没问题,同事的浏览器可以加载该js,发现放到其他的文件夹下可以加载该js:当时没研究出来,任务紧就没研究了. 最近闲下来了,有去研究, ...
- CocoaPods安装/卸载/初始化等常用操作
CocoaPods的官网:https://cocoapods.org/,官方指导文档https://guides.cocoapods.org/ 1)ruby gem源更换国内源gems.ruby-ch ...
- JSTL与EL表达式(为空判断)
JSTL与EL表达式(为空判断) 一.循环遍历集合 1.在jsp中引入标准函数声明 <%@ taglib uri="http://java.sun.com/jsp/jstl/cor ...
- python *args 和 ** kwargs
可变长度的参数 *args的使用方法 *args 用来将参数打包成tuple给函数体调用 可见,1这个参数,被打包成了一个元组 def func(*args): print(args,type(arg ...
- ansible 文本多行替换实例
将<level='info'> 等全部替换为<level='ERROR'> - name: Before Ansible 2.3, option 'dest', 'destfi ...
- Teamviewer 退出时设置不锁屏
在pc端teamviewer登录同伴的机器时,页面最上端会出现一个工具栏,如图: 点击"动作" ---〉"锁定" 取消打钩 "在会话结束时锁定&qu ...
- eclipse JSP学习遇到的问题,获取页面中文值时出现乱码
性别:男<input type="radio" name="sex" value="男" /> String sex =requ ...