【字符串】PAT甲(求公共后缀)、PAT甲 1069整型转字符串,字符串转整型、牛客 子串计算(substr)、牛客 字符串排序(stable_sort)

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?

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 <stdio.h>
#include <cstdio>
#include <string>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
using namespace std;    //给出N行字符串,输出它们的公共后缀,如果没有输出nai
int main(){
    int n;
    scanf("%d",&n);
    string str[101];
    int min=99999;
      getchar();
    for(int i=0;i<n;i++)
    {
      
        getline(cin,str[i]);
        int len=str[i].length();
       // printf("%d ",len);
        if(len<min) min=len;
        reverse(str[i].begin(),str[i].end());
//        为了方便比较,可以将每一行的字符串都进行一次翻转,求出公共后缀后再翻转回来。
//        再输入的时候求出最小的字符串长度作为循环截止条件。
       // cout<<str[i]<<endl;
    }
   // printf("%d ",min);
    int cnt=0;
    for(int j=0;j<min;j++)
    {
        int flag=0;
        int c=str[0][j];
        for(int i=1;i<n;i++)
        {
            if(str[i][j]!=c)
            {
                flag=1;
                break;
            }
        }
        if(flag==1)
        {
            cnt=j;
            break;
        }
        else
        {
            cnt++;
        }
    }
    //printf("%d ",cnt);
    if(cnt!=0)
    {
        for(int i=cnt-1;i>=0;i--)
        printf("%c",str[0][i]);
    }
    else{
        printf("nai");
    }
}

 

1069 The Black Hole of Numbers (20 分)

For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in non-increasing order first, and then in non-decreasing order, a new number can be obtained by taking the second number from the first one. Repeat in this manner we will soon end up at the number 6174 -- the black hole of 4-digit numbers. This number is named Kaprekar Constant.

For example, start from 6767, we'll get:

7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174
7641 - 1467 = 6174
... ...

Given any 4-digit number, you are supposed to illustrate the way it gets into the black hole.

Input Specification:

Each input file contains one test case which gives a positive integer N in the range (0,10​4​​).

Output Specification:

If all the 4 digits of N are the same, print in one line the equation N - N = 0000. Else print each step of calculation in a line until 6174 comes out as the difference. All the numbers must be printed as 4-digit numbers.

Sample Input 1:

6767

Sample Output 1:

7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174

Sample Input 2:

2222

Sample Output 2:

2222 - 2222 = 0000

#include <stdio.h>
#include <cstdio>
#include <string>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
using namespace std;
bool cmp1(char a,char b)
{
    return a>b;
}
bool cmp2(char a,char b)
{
    return a<b;
}
int main(){
    int n;
    scanf("%d",&n);
    do
    {
        char a[5];
        sprintf(a,"%d",n);
        int len=strlen(a);
        while(len<4)
        {
            a[len++]='0';
        }
        sort(a,a+4,cmp1);
        int s1,s2;
        sscanf(a,"%d",&s1);
        sort(a,a+4,cmp2);
        sscanf(a,"%d",&s2);
        n=s1-s2;
        printf("%04d - %04d = %04d\n",s1,s2,n);
        if(n==6174)
            break;
        if(n==0)
            break;
    }while(n!=6174||n!=0);
}

 

 

题目描述

给出一个01字符串(长度不超过100),求其每一个子串出现的次数。

输入描述:

输入包含多行,每行一个字符串。

输出描述:

对每个字符串,输出它所有出现次数在1次以上的子串和这个子串出现的次数,输出按字典序排序。

示例1

输入

复制

10101

输出

复制

0 2
01 2
1 3
10 2
101 2

#include <stdio.h>
#include <cstdio>
#include <string>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#include <stack>
#include <map>
using namespace std;
int main(){
    string s;
    while(cin>>s)
    {
        int len=s.length();
        map<string,int>mmap;
        for(int i=0;i<len;i++)
        {
            for(int j=i;j<len;j++)
            {
                mmap[s.substr(i,j-i+1)]++;
            }
        }
        map<string,int>::iterator it;
        for(it=mmap.begin();it!=mmap.end();it++)
        {
            if(it->second>1)
            cout<<it->first<<" "<<it->second<<endl;
        }
    }
}

 

题目描述

编写一个程序,将输入字符串中的字符按如下规则排序(一个测试用例可能包含多组数据,请注意处理)。

规则 1 :英文字母从 A 到 Z 排列,不区分大小写。

如,输入: Type 输出: epTy

规则 2 :同一个英文字母的大小写同时存在时,按照输入顺序排列。

如,输入: BabA 输出: aABb

规则 3 :非英文字母的其它字符保持原来的位置。

如,输入: By?e 输出: Be?y

样例:

输入:

A Famous Saying: Much Ado About Nothing(2012/8).

输出:

A aaAAbc dFgghh : iimM nNn oooos Sttuuuy (2012/8).

 

输入描述:

 

 

输出描述:

 

 

示例1

输入

复制

A Famous Saying: Much Ado About Nothing (2012/8).

输出

复制

A aaAAbc dFgghh: iimM nNn oooos Sttuuuy (2012/8).

#include <stdio.h>
#include <cstdio>
#include <string>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
using namespace std;
bool cmp(char a,char b)
{
    if(a>='A'&&a<='Z')
    {
        a=a-'A'+'a';
    }
    if(b>='A'&&b<='Z')
    {
        b=b-'A'+'a';
    }
    return a<b;
}
int main(){
    char str[1001],str1[1001];
    while(gets(str))
    {
        //cout<<str<<endl;
        int len=strlen(str);
        int mark[1001];
        for(int i=0;i<1001;i++)
            mark[i]=0;
        int k1=0;
        for(int i=0;i<len;i++)
        {
            if((str[i]>='A'&&str[i]<='Z')||(str[i]>='a'&&str[i]<='z'))
                str1[k1++]=str[i];
            else
                mark[str[i]]=1;
        }
        //cout<<str1<<endl;
        stable_sort(str1,str1+k1,cmp);
        int k2=0;
        for(int i=0;i<len;i++)
        {
            if(mark[str[i]]==1)
            {
                printf("%c",str[i]);
            }
            else
            {
                printf("%c",str1[k2]);
                k2++;
            }
        }
        printf("\n");
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值