PAT甲级_字符串

A1005 Spell It Right (20 分)

原题

Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English.
Input Specification:
Each input file contains one test case. Each case occupies one line which contains an N ( ≤ 1 0 ​ 100 ​ ​ ) N(≤10^​100​​) N(10100).
Output Specification:
For each test case, output in one line the digits of the sum in English words. There must be one space between two consecutive words, but no extra space at the end of a line.
Sample Input:
12345
Sample Output:
one five

解题

简单题,读入为字符串数组,或者读入单个字符直接处理成数,并累加。累加结果to_string()转换为字符串,然后switch转换为对应的英文单词。转换这一步也可以提前构造map<int,string>,然后直接用数字取键值对即可。
github link

A1035 Password (20 分)

原题

To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem is that there are always some confusing passwords since it is hard to distinguish 1 (one) from l (L in lowercase), or 0 (zero) from O (o in uppercase). One solution is to replace 1 (one) by @, 0 (zero) by %, l by L, and O by o. Now it is your job to write a program to check the accounts generated by the judge, and to help the juge modify the confusing passwords.

Input Specification:
Each input file contains one test case. Each case contains a positive integer N (≤1000), followed by N lines of accounts. Each account consists of a user name and a password, both are strings of no more than 10 characters with no space.

Output Specification:
For each test case, first print the number M of accounts that have been modified, then print in the following M lines the modified accounts info, that is, the user names and the corresponding modified passwords. The accounts must be printed in the same order as they are read in. If no account is modified, print in one line There are N accounts and no account is modified where N is the total number of accounts. However, if N is one, you must print There is 1 account and no account is modified instead.

Sample Input 1:
3
Team000002 Rlsp0dfa
Team000003 perfectpwd
Team000001 R1spOdfa
Sample Output 1:
2
Team000002 RLsp%dfa
Team000001 R@spodfa
Sample Input 2:
1
team110 abcdefg332
Sample Output 2:
There is 1 account and no account is modified
Sample Input 3:
2
team110 abcdefg222
team220 abcdefg333
Sample Output 3:
There are 2 accounts and no account is modified

解题

  1. 数据的存储仍然有两种选择,class 和map<string,string>,前者更顺手方便,而且可以重载运算符<<。
  2. 主要任务就是替换密码字段字符,替换发生则输出发生了替换的用户及其密码和总数,未发生替换或者只有一位用户时输出提示信息
  3. 第一次提交有一个样例错误,是因为没有注意到题目要求

However, if N is one, you must print There is 1 account and no account is modified instead.

github link

A1061 Dating (20 分)

原题

Sherlock Holmes received a note with some strange strings: Let’s date! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm. It took him only a minute to figure out that those strange strings are actually referring to the coded time Thursday 14:04 – since the first common capital English letter (case sensitive) shared by the first two strings is the 4th capital letter D, representing the 4th day in a week; the second common character is the 5th capital letter E, representing the 14th hour (hence the hours from 0 to 23 in a day are represented by the numbers from 0 to 9 and the capital letters from A to N, respectively); and the English letter shared by the last two strings is s at the 4th position, representing the 4th minute. Now given two pairs of strings, you are supposed to help Sherlock decode the dating time.

Input Specification:
Each input file contains one test case. Each case gives 4 non-empty strings of no more than 60 characters without white space in 4 lines.

Output Specification:
For each test case, print the decoded time in one line, in the format DAY HH:MM, where DAY is a 3-character abbreviation for the days in a week – that is, MON for Monday, TUE for Tuesday, WED for Wednesday, THU for Thursday, FRI for Friday, SAT for Saturday, and SUN for Sunday. It is guaranteed that the result is unique for each case.

Sample Input:
3485djDkxh4hhGE
2984akDfkkkkggEdsb
s&hgsfdk
d&Hyscvnm
Sample Output:
THU 14:04

解题

  1. 题意需要仔细理解,四个字符串中要找三个要素。前两个字符串中找第一个公共大写字母,为星期,第二个为小时。后两个字符串中找第一个同位置的公共字母,为分钟。
  2. 核心算法就是遍历字符串,遍历string1找大写字母,找到后在string2中find(),
    github link

A1071 Speech Patterns (25 分)

原题

People often have a preference among synonyms of the same word. For example, some may prefer “the police”, while others may prefer “the cops”. Analyzing such patterns can help to narrow down a speaker’s identity, which is useful when validating, for example, whether it’s still the same person behind an online avatar.

Now given a paragraph of text sampled from someone’s speech, can you find the person’s most commonly used word?

Input Specification:
Each input file contains one test case. For each case, there is one line of text no more than 1048576 characters in length, terminated by a carriage return \n. The input contains at least one alphanumerical character, i.e., one character from the set [0-9 A-Z a-z].

Output Specification:
For each test case, print in one line the most commonly occurring word in the input text, followed by a space and the number of times it has occurred in the input. If there are more than one such words, print the lexicographically smallest one. The word should be printed in all lower case. Here a “word” is defined as a continuous sequence of alphanumerical characters separated by non-alphanumerical characters or the line beginning/end.

Note that words are case insensitive.

Sample Input:
Can1: “Can a can can a can? It can!”
Sample Output:
can 5

解题

  1. 核心人物就是一行字符串拆分为单词(本题中单词定义为连续的数字字母混编字符串),然后统计词频,输出字母序最小和词频最高的单词。
  2. 统计词频选用遍历单词表,并存在set中,已存在的key则value++,否则存入新的k-v对,value初始为1。
  • 在set中查找当前单词的方法
    • 自写遍历set的函数,找到返回true,否则返回false。
    • 利用set::at()函数,不存在该单词时返回out_of_range异常,需要使用try{}catch{}来处理。
    • 算法库的find()函数是不能用在set上的,但是有set::find(),不存在时返回set::end()
  1. 注意,本题不分大小写。
  2. 单词表的排序需要对map的key和value双关键字排序。
  3. 遇到一个样例出现段错误。修改for (string::iterator it = s.begin(); it != s.end(); it++)for (string::iterator it = s.begin(); it < s.end(); it++)后AC。it是随机存取迭代器,可以用比较符。
    github link

※ A1082 Read Number in Chinese (25 分)

原题

Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chinese way. Output Fu first if it is negative. For example, -123456789 is read as Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu. Note: zero (ling) must be handled correctly according to the Chinese tradition. For example, 100800 is yi Shi Wan ling ba Bai.
Input Specification:
Each input file contains one test case, which gives an integer with no more than 9 digits.
Output Specification:
For each test case, print in a line the Chinese way of reading the number. The characters are separated by a space and there must be no extra space at the end of the line.
Sample Input 1:
-123456789
Sample Output 1:
Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu
Sample Input 2:
100800
Sample Output 2:
yi Shi Wan ling ba Bai

解题

  1. 核心问题在于总结出中文读数字的规律,基本上是四位分一个字段,段内组成9999以内的数字。段内或者段间有0占位时多读一个零。段间的零放在低段处理,有高段时再加。
  2. 实际解题中还发现了几个问题
  • 想要统一处理每一段,那么要去每段长度一致,所以读入的字符串要标准化,左补0。若补到9位,则亿字段单独处理,若补到12位,则每一段都一样处理。
  • 读每一段参数用字符串比用int向量方便,因为字符串可以直接从原始字符串取下相应子串即可
  • 零的读法:要求更高位均不为零,更低位至少有一个不为零
  • 循环的控制条件最好是常数
/* 
for (int i = 0; i < 9 - num_s.size(); i++)
	num_s = "0" + num_s; 
这样设置循环终止条件是有问题的
*/	
int lim = 9 - num_s.size();
for (int i = 0; i < lim; i++)
	num_s = "0" + num_s;

github link

A1112 Stucked Keyboard (20 分)

原题

On a broken keyboard, some of the keys are always stucked. So when you type some sentences, the characters corresponding to those keys will appear repeatedly on screen for k times.

Now given a resulting string on screen, you are supposed to list all the possible stucked keys, and the original string.

Notice that there might be some characters that are typed repeatedly. The stucked key will always repeat output for a fixed k times whenever it is pressed. For example, when k=3, from the string thiiis iiisss a teeeeeest we know that the keys i and e might be stucked, but s is not even though it appears repeatedly sometimes. The original string could be this isss a teest.

Input Specification:
Each input file contains one test case. For each case, the 1st line gives a positive integer k (1<k≤100) which is the output repeating times of a stucked key. The 2nd line contains the resulting string on screen, which consists of no more than 1000 characters from {a-z}, {0-9} and _. It is guaranteed that the string is non-empty.

Output Specification:
For each test case, print in one line the possible stucked keys, in the order of being detected. Make sure that each key is printed once only. Then in the next line print the original string. It is guaranteed that there is at least one stucked key.

Sample Input:
3
caseee1__thiiis_iiisss_a_teeeeeest
Sample Output:
ei
case1__this_isss_a_teest

解题

  1. 核心任务就是替换符合要求的字符串,要求是k个相同的字符一组,而且这个字符出现时只能以组的形式出现,每组替换为一个字符。
  2. 出错仍然在理解题意上。第一次的方案,将出现过至少两次的组都认定为故障键,这显然不符合故障键只要出现就要成组的要求。更改后正确。
    github link
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值