【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

题目大意

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

思路简介

最长公共后缀

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

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

遇到的问题

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()
相关推荐
段一凡-华北理工大学2 小时前
AI推动工业智能化转型~系列文章20:工业 AI 平台架构:云-边-端协同的技术体系
人工智能·python·架构·工业平台·云-边协同
CodeBlog-star2 小时前
Harness Engineering:Pi Agent 架构深度解析
人工智能·python·架构·harness工程
简不变2 小时前
将components下的文件夹复制到别一个文件夹下,并重新生成CMakelist.txt文件
python
库玛西3 小时前
现代 C++ 智能指针全景指南:从 RAII 思想到工业级实践
c语言·开发语言·c++·笔记·面试
AC赳赳老秦3 小时前
风控岗应用:OpenClaw 采集公开司法与经营异常数据,自动生成企业风险评估报告
大数据·c语言·数据库·人工智能·python·php·openclaw
databook3 小时前
如何对稀疏数据的场景进行分析
python·数据挖掘·数据分析
liulilittle3 小时前
无锁并发容器的设计与实现原理
开发语言·c++·set·map·并发·无锁·lock-free
qeen874 小时前
【数据结构】自平衡二叉搜索树各种旋转算法原理解析及AVL树的C++实现
数据结构·c++·算法
lucas_AI4 小时前
1.2B 小模型赢过 235B 大模型:NaviDC-OCR 把文档解析卷明白了
人工智能·深度学习·算法
Java&Develop4 小时前
DeltaFStation 宝塔 Linux 部署教程
python