【PAT甲级真题】- Kuchiguse (20)

题目来源

Kuchiguse - PTA

Kuchiguse - 牛客

注意点:

  • 有部分输入的细节,见后文

Description

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 ) N (2≤N≤100) N(2≤N≤100). Following are N N N file lines of 0 256 0~256 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 N 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

题目大意

给出若干字符串,输出它们的最长公共后缀

思路简介

最长公共后缀

用第一个字符串与后续的字符串从后往前以一比对即可

python 的 os 库提供字符串公共前缀函数用于文件处理,可以把字符串倒转后求公共前缀来解题

遇到的问题

c++版

  1. 从末尾开始比对字符时,注意记录的是匹配失败的位置,要加 1 1 1 才是匹配成功的位置

代码

cpp 复制代码
/**
 * https://www.nowcoder.com/pat/5/problem/4307
 * https://pintia.cn/problem-sets/994805342720868352/exam/problems/type/7?problemSetProblemId=994805390896644096
 * 最长后缀匹配
 */
#include<bits/stdc++.h>
using namespace std;

string s[200];
void solve(){
    int n;
    cin>>n;
    cin.ignore();//避免读取换行
    
    for(int i=0;i<n;++i)
        getline(cin,s[i]);
    int p=0;

    for(int i=1;i<n;++i){
        int k1=s[0].size()-1;
        int k2=s[i].size()-1;
        while(k1>=0&&k2>=0&&s[i][k2]==s[0][k1])
            k1--,k2--;
        p=max(p,k1+1);//注意k1是对比后减去1的位置,加回1才是配对的位置
    }
    if(p==s[0].size())cout<<"nai\n";
    else {
        cout<<s[0].substr(p)<<'\n';
    }
}
int main(){
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //fstream in("in.txt",ios::in);cin.rdbuf(in.rdbuf());
    int T=1;
    //cin>>T;
    while(T--){
        solve();
    }
    return 0;
}
python 复制代码
import os

def common_suffix(strs):#输入字符串列表
    strs_reverse=[s[::-1] for s in strs]#构造反转字符串的列表
    res=os.path.commonprefix(strs_reverse)#获取文件路径公共前缀
    if(res):return res[::-1]
    else: return "nai"


import sys
def main():
    #sys.stdin = open('in.txt')          # 本地测试用,提交时注释掉
    n = int(input())                    # 读取整数
    lines = [input() for _ in range(n)] # 读取 n 行字符串(可含空格)
    res=common_suffix(lines)
    print(res)

    
if __name__=="__main__":
    main()
相关推荐
禁默25 分钟前
论文解读与思考:基于场所地图的标识感知机器人探索方法及工程参考的价值
人工智能·深度学习·算法·机器人
维克兜率天25 分钟前
【维克】统计套利建模:从因子到信号的完整流程
android·开发语言·python·算法·kotlin
全栈练习生28 分钟前
RAG 准确率提升体系化方案
python·ai
Zentceh32 分钟前
PixelClean 这类 AI 降噪算法是怎么工作的?从 RAW 域到 GAN 色彩重建的技术拆解
图像处理·人工智能·科技·算法·计算机视觉·车载系统·智能硬件
用户76241175670533 分钟前
第十一章 GUI界面编程
python
天天被压力35 分钟前
【Python量化实战宝典 #03】指数与板块行情接口全拆解:大盘·行业·概念一键取(Python 可跑)
java·人工智能·python
小马不起床36 分钟前
第七阶段:数据存储与部署
数据库·python
用户03906147071937 分钟前
无感观测器理论模型(NTSM-SMO-PLL 与 SMO-PLL)
算法
weixin_461769401 小时前
conda create 创建容器 Failed to resolve ‘repo.anaconda.com‘问题
开发语言·python·conda·repo·网络问题·无法创建
Want5951 小时前
Python生日蛋糕
开发语言·python