PAT (Advanced level) 1077. Kuchiguse (20) 最长共同后缀

来源:互联网 发布:西部数码域名管理地址 编辑:程序博客网 时间:2024/05/29 09:13

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:
    3Itai nyan~Ninjin wa iyadanyan~uhhh nyan~
    Sample Output 1:
    nyan~
    Sample Input 2:
    3Itai!Ninjinnwaiyada T_TT_T
    Sample Output 2:
    nai
    从后往前遍历。
    /*2015.7.29cyq*/#include <iostream>#include <string>#include <vector>#include <fstream>#include <cstring>using namespace std;//ifstream fin("case1.txt");//#define cin finint main(){int N;cin>>N;string enter;getline(cin,enter);//去掉第一行的回车vector<string> svec(N);vector<int> len(N);for(int i=0;i<N;i++){getline(cin,svec[i]);len[i]=svec[i].size();}int count=0;bool flag;for(int i=1;i<=len[0];i++){flag=true;for(int j=1;j<N;j++){//第二个string及之后的stringif(len[j]<i||svec[j][len[j]-i]!=svec[0][len[0]-i]){flag=false;break;}}if(!flag)break;count++;}if(count==0)cout<<"nai";elsecout<<svec[0].substr(len[0]-count);return 0;}


  • 0 0
    原创粉丝点击
    热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 电脑lp地址错误怎么办 录屏音画不同步怎么办 360浏览器无响应怎么办 360一直未响应怎么办 手机qq无法打开怎么办 电脑打开直接蓝屏怎么办 电脑在屏保界面怎么办 bak文件无法打开怎么办 小米4连不上wifi怎么办 360借条账户异常怎么办 360账户状态异常怎么办 手机qq联网超时怎么办 腾讯视频被盗号怎么办 pos机连接超时怎么办 qq出现系统错误怎么办 校园宽带登录超时怎么办 微信登录频繁怎么办 小黄车登录不上怎么办 快刷服务器超时怎么办 连接isp服务器超时怎么办 Qq后端服务超时怎么办 cf连接服务器失败怎么办 手机qq登录超时怎么办 qq后端服务器超时怎么办 游戏中登录超时怎么办 802.1x认证超时怎么办 连接认证服务器超时怎么办 登录企业qq超时怎么办 苹果手机下载不了怎么办 信用卡有溢存款怎么办 信用卡余额多了怎么办 信用卡还了退款怎么办 京东京豆是负的怎么办 京豆为负数怎么办 手机应用程序未响应怎么办 手机号换了淘宝怎么办 移动号码被锁定怎么办 联通号码被锁定怎么办 扣扣密保手机不用了怎么办 手机号被回收了怎么办 qq小游戏没有声音怎么办