1077. Kuchiguse (20) 思路相同 ******

简介: The Japanese language is notorious for its sentence ending particles.
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 <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 = (int)s.length();
        reverse(s.begin(), s.end());
        if(i == 0) {
            ans = s;
            continue;
        } else {
            int lenans = (int)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;
}

目录
相关文章
|
6月前
|
算法
动态规划的思路
动态规划的思路
|
11月前
|
算法
写题思路的分享
写题思路的分享
52 0
|
机器学习/深度学习 存储 缓存
力扣70爬楼梯:思路分析+优化思路+代码实现+补充思考
力扣70爬楼梯:思路分析+优化思路+代码实现+补充思考
145 0
力扣面试题 08.06. 汉诺塔问题:思路分析+图文详解+代码实现
力扣面试题 08.06. 汉诺塔问题:思路分析+图文详解+代码实现
172 0
|
算法 C++ 容器
Leetcode 算法解题思路总结
Leetcode 算法解题思路总结
|
算法 Go 开发者
二分查找的思路分析|学习笔记
快速学习二分查找的思路分析
二分查找的思路分析|学习笔记
|
物联网 数据库 知识图谱
[视频]理思路|学习笔记(二)
快速学习[视频]理思路|学习笔记
|
物联网 开发者 智能硬件
|
算法 Go 开发者
冒泡排序思路分析 | 学习笔记
简介:快速学习冒泡排序思路分析
102 0
|
人工智能 算法 C++
Leetcode第1题至第10题 思路分析及C++实现
笔者按照目录刷题,对于每一道题,力争使用效率最高(时间复杂度最低)的算法,并全部通过C++代码实现AC。(文中计算的复杂度都是最坏情况复杂度) 因为考虑到大部分读者已经在Leetcode浏览过题目了,所以每道题都按照 解题思路 -> 实现代码 -> 问题描述 的顺序进行讲解。
1409 0