【第22期】观点:IT 行业加班,到底有没有价值?

PAT甲级.1077. Kuchiguse (20)

原创 2016年08月31日 10:54:05

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?

输入格式

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.

输出格式

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”.

输入样例1

3
Itai nyan~
Ninjin wa iyadanyan~
uhhh nyan~

输出样例1

nyan~

输入样例2

3
Itai!
Ninjinnwaiyada T_T
T_T

输出样例2

nai

PAT链接


思路

1.取第一个读取的字符串反转后存入ans[]内,之后对每个读入的字符串反转之后与ans从头到后比较,发现不同字符跳出,将之前相同的字符串再更新到ans中
2.将ans反转输出


代码

/**
* @tag     PAT_A_1077
* @authors R11happy (xushuai100@126.com)
* @date    2016-8-30 23:56-00:30
* @version 1.0
* @Language C++
* @Ranking  380/2402
* @function null
*/

#include <cstdio>
#include <cstdlib>
#include <cstring>

char str[110][260];

//反转字符串
void reverse(char s[])
{
    int len = strlen(s);
    for (int i = 0; i<len / 2; i++)
    {
        int tmp = s[i];
        s[i] = s[len - 1 - i];
        s[len - 1 - i] = tmp;
    }
}

int main(int argc, char const *argv[])
{
    int N,j;
    char ans[260];
    scanf("%d", &N);
    getchar();
    gets(ans);
    reverse(ans);
    for (int i = 1; i<N; i++)
    {
        char tmp[260];
        gets(str[i]);
        reverse(str[i]);
        int len_ans = strlen(ans);
        int len_str = strlen(str[i]);
        int len = len_ans > len_str ? len_str : len_ans;
        for (j = 0; j<len; j++)
        {
            if (ans[j] == str[i][j])    tmp[j] = ans[j];
            else break;
        }
        tmp[j] = '\0';  //注意,不是tmp[++j]
        strcpy(ans, tmp);
    }
    reverse(ans);
    if (strlen(ans)) puts(ans);
    else    printf("nai\n");
    return 0;
}

收获

1.对于从后向前比较的字符串,先反转再操作的话会比较简单

//反转字符串
void reverse(char s[])
{
    int len = strlen(s);
    for (int i = 0; i<len / 2; i++)
    {
        int tmp = s[i];
        s[i] = s[len - 1 - i];
        s[len - 1 - i] = tmp;
    }
}

2.字符串注意处理最后的’\0’

        tmp[j] = '\0';  //注意,不是tmp[++j]
版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

PAT(甲级)1077. Kuchiguse (20)

题目:https://www.patest.cn/contests/pat-a-practise/1077 代码:#include #include #include #include using ...

PAT 1015. Reversible Primes (20)

#include&lt;stdio.h&gt; int main(){ int n, d; int reversiblePrime = 0; while(scanf("%d%d\n", &amp;n,&amp;d) == 2 &...

程序员升职加薪指南!还缺一个“证”!

CSDN出品,立即查看!

PAT 甲级 1077. Kuchiguse

原来我以为是找N个字符串的最大公共字串,有点懵逼,后来看答案才发现,是公共字串是在最后出现的。 然后我也看了答案,就把答案贴上来吧。。。 有个陷阱,就是读取字符串的最后可能会有空格。 #in...

pat 1019. General Palindromic Number (20)

链接:http://pat.zju.edu.cn/contests/pat-a-practise/1019   题意:给定一个十进制数和一个进制,判断该数字在该进制下是否为回文。   分析:简单的数字进制处理。  </

1077. Kuchiguse (20) PAT甲级

传送门#include #include using namespace std;vector v;int main(){ int N; string s; int minLe...
  • SY_Yu
  • SY_Yu
  • 2017-01-18 09:57
  • 79
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)