凤湖中学在这学期流行一个名为“三人行”英文诗赛。比赛中,"老师"先说一个英语单词作为主题词,然后三位“路人"各说一句英语诗,看谁的诗更符合主题词。以主题词中的字母在诗中的出现次数作为符合度。在评价符合度时:不区分大小写;只关注英语字母(a-z),忽略其它字母。如果某句诗所含英语字母数大于20个,则分别用该诗句的前20个英语字母和后20个英语字母来计算符合度,并取两者之较大值作为实际的符合度。
输入格式:
输入一共有4行,第1行是一个主题词,接下来的3行,各输入一句英语诗句。
输出格式:
输出一共有3行,是各句诗的符合度。
输入样例:
soon
Some tours are doomed to return, Some doors are destined to open then to close.
Days fly by. Soon I need to remove this page, and hang up a new year.
Summer isn't far behind, And another year has passed.
输出样例:
8
7
5
处理过程
主题词: soon
- 转换为小写:已经是小写。
诗句1: Some tours are doomed to return, Some doors are destined to open then to close.
- 只保留英语字母并转换为小写:
sometoursaredoomedtoreturnsomedoorsaredestinedtoopenthentoclose - 由于字母数超过20个,我们需要考虑前20个字母
sometoursaredoomedtore和后20个字母edtoopenthenotoclose。 - 前20个字母中,
s出现3次,o出现5次,n未出现,总符合度为8。 - 后20个字母中,
s出现1次,o出现4次,n出现1次,总符合度为6。 - 取较大值:8。
诗句2: Days fly by. Soon I need to remove this page, and hang up a new year.
- 只保留英语字母并转换为小写:
daysflybysoonineedtoremovethispageandhangupanewyear - 由于字母数超过20个,我们需要考虑前20个字母
daysflybysoonineedtor和后20个字母geandhangupanewyear。 - 前20个字母中,
s出现3次,o出现3次,n出现1次,总符合度为7。 - 后20个字母中,
s出现1次,o出现1次,n出现1次,总符合度为3。 - 取较大值:7。
诗句3: Summer isn't far behind, And another year has passed.
- 只保留英语字母并转换为小写:
summerisntfarbehindandanotheryearhaspassed - 由于字母数超过20个,我们需要考虑前20个字母
summerisntfarbehinda和后20个字母ndanotheryearhaspassed。 - 前20个字母中,
s出现2次,o未出现,n出现2次,总符合度为4。 - 后20个字母中,
s出现2次,o未出现,n出现3次,总符合度为5。 - 取较大值:5。
评测详情
| 测试点 | 提示 | 内存(KB) | 用时(ms) | 结果 | 得分 | |
| 0 | 308 | 2 | 答案正确 | 4 / 4 | ||
| 1 | 300 | 2 | 答案正确 | 2 / 2 | ||
| 2 | 424 | 3 | 答案正确 | 2 / 2 | ||
| 3 | 500 | 3 | 答案正确 | 2 / 2 | ||
| 4 | 292 | 3 | 答案正确 | 1 / 1 | ||
| 5 | 316 | 2 | 答案正确 | 1 / 1 |
代码如下:
#include <iostream>
#include <string>
using namespace std;
bool flag[100] = {false};
int main()
{
string str;
cin >>str;
getchar();
char c1[100];
int m = 0;
for(int i=0;i<str.size();i++){
if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z')){
if(str[i]>='A'&&str[i]<='Z') str[i] = str[i] - 'A' + 'a';
if(!flag[str[i]]){
c1[m++] = str[i];
}
flag[str[i]] = true;
}
}
for(int i=0;i<3;i++){
string s;
char c[110];
getline(cin,s);
int cnt = 0,h = 0;
for(int i=0;i<s.size();i++){
if((s[i]>='a'&&s[i]<='z')||(s[i]>='A'&&s[i]<='Z')){
if(s[i]>='A'&&s[i]<='Z') s[i] = s[i] - 'A' + 'a';
cnt++;
c[h++] = s[i];
}
}
int cnt1 = 0,cnt2 = 0;
if(cnt<=20){
for(int i=0;i<h;i++){
for(int j=0;j<m;j++){
if(c[i] == c1[j]) cnt1++;
}
}
cout <<cnt1<<endl;
continue;
}
else {
for(int i=0;i<20;i++){
for(int j=0;j<m;j++)
if(c[i] == c1[j]) cnt1++;
}
for(int i = h-1;i>=h-20;i--){
for(int j = 0;j<m;j++){
if(c[i] == c1[j]) cnt2 ++;
}
}
if(cnt1>cnt2) cout <<cnt1<<endl;
else cout <<cnt2<<endl;
}
}
return 0;
}
可能有点繁琐,应该有更加简洁的方法
文章讲述了凤湖中学举办的一场英文诗赛,比赛规则是根据诗句中与主题词相同字母的数量计算符合度,特殊情况下会考虑诗句前后20个字母。
1189





