2022.7.9暑假个人训练1-B.How old are you Mr. String

题解报告写的有点晚了,也属实有点偷懒了,今天开始补一下之前的题解报告。

B . How old are you Mr. String

题目描述

Given two strings, each consisting of only the lower-case letters,you are to compare their age. String1 is considered older than string2 if String1 has more occurrences of the letter 'z' than String2 does. If both strings have the same number of z's, string1 is older if it has more y's. If they have the same number of y's, then number of x's determine the older string, etc. If the two strings have the same number of z's, the same number of y's, ..., thesame number of a's, then the two strings are considered to be the same age.

输入描述

The first input line contains a positive integer, nn, indicating the number of data sets (how many pairs of strings are to be compared). This is followed by 2n2n input lines, each data set consisting of two lines (strings). Assume that each string is at least one and at most 70 letters, starts in column one and contains no other characters.

输出描述

For each data set, print the heading"Data set #ii: " where ii is the number for the set (starting with 1). Then,print one of the following three messages:

First string is older

First string is younger

The two strings are the same age

Leave a blank line after the output for each data set. Follow the format illustrated in Sample Output.

样例输入

3
yzzz
abcxyz
ay
xy
aliorooji
oroojiali

样例输出:

Data set #1: First string is older

Data set #2: First string is younger

Data set #3: The two strings are the same age

题目大意:输入n组,每组两个字符串,比较两个字符串,规则:从‘z’开始比,数量少的younger,如果比到’a‘时还没有比较出来,则认为两个一样

思路:定义两个数组来记录各字母的个数,然后从后开始比较。

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int cnt1[30];
int cnt2[30];

int main()
{
    int t;
    cin >> t;
    for(int ii = 1;ii <= t;ii++){
        string s1,s2;
        cin >> s1 >>s2;
        memset(cnt1,0,sizeof(cnt1));
        memset(cnt2,0,sizeof(cnt2));
        for(int i = 0;i < s1.size();i++)
            cnt1[s1[i]-'a']++;
        for(int i = 0;i < s2.size();i++)
            cnt2[s2[i]-'a']++;
        int flag = 0;
        for(int i = 25;i >= 0;i--){
            if(cnt1[i] == cnt2[i])
                continue;
            else if(cnt1[i] > cnt2[i]){
                flag = 1;
                break;
            }
            else if(cnt1[i] < cnt2[i]){
                flag = 2;
                break;
            }
        }
        cout << "Data set #" << ii << ": ";
        if(flag == 0)
            cout << "The two strings are the same age" << endl << endl;
        else if(flag == 1)
            cout << "First string is older" << endl << endl;
        else
            cout << "First string is younger" << endl << endl;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值