愉快的补题解1 CodeForces - 877A Alex and broken contest 大大大大大水题

本文介绍了一个字符串匹配的问题,任务是判断给定的字符串是否恰好包含指定的五个名字之一。通过遍历字符串并使用substr函数进行匹配,最终输出是否满足条件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

One day Alex was creating a contest about his friends, but accidentally deleted it. Fortunately, all the problems were saved, but now he needs to find them among other problems.

But there are too many problems, to do it manually. Alex asks you to write a program, which will determine if a problem is from this contest by its name.

It is known, that problem is from this contest if and only if its name contains one of Alex’s friends’ name exactly once. His friends’ names are “Danil”, “Olya”, “Slava”, “Ann” and “Nikita”.

Names are case sensitive.

Input
The only line contains string from lowercase and uppercase letters and “_” symbols of length, not more than 100 — the name of the problem.

Output
Print “YES”, if problem is from this contest, and “NO” otherwise.

Examples
Input
Alex_and_broken_contest
Output
NO

Input
NikitaAndString
Output
YES
Input
Danil_and_Olya
Output
NO

题目大意:给出一个串,若串中只包含一个已知串便满足规则。
思路:一共只有100个字符,以每个字符为起点,遍历即可。

AC代码

#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>

using namespace std;
 
string str[5] = {"Danil", "Olya", "Slava", "Ann" , "Nikita"};
 
string S;
 
int main(){
	while (cin >> S){
		int num = 0;
		for (int i = 0; i < 5; i++) {
			for (int j = 0; j < S.size(); j++) {
				if (S.substr(j, str[i].size()) == str[i])
					num++;
			}
		}
		if (num == 1){
			cout<<"YES\n";
		}else{
			cout<<"NO\n";
		}
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值