Alex and broken contest (字符串)CodeForces - 877A

第五次个人赛

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

问题:给一个字符串s,然后找出字符串里面仅出现一次的 Alex朋友的姓名,超过两次或没有输出NO,出现一次输出YES

三种方法:

第一种简单暴力:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <map>
#include <cstring>
using namespace std;
typedef long long LL;
int main()
{
    string a="Danil",b="Olya",c="Slava",d="Ann",e="Nikita";
    string s;
    int sum;
    while(cin>>s)
    {
        sum=0;
        int aa=0,bb=0,cc=0,dd=0,ee=0;
        for(int i=0; i<s.size(); i++)
        {
            if(s[i]=='D'&&s[i+1]=='a'&&s[i+2]=='n'&&s[i+3]=='i'&&s[i+4]=='l')
            {
                aa++;
            }
            if(s[i]=='O'&&s[i+1]=='l'&&s[i+2]=='y'&&s[i+3]=='a')
            {
                bb++;
            }
            if(s[i]=='S'&&s[i+1]=='l'&&s[i+2]=='a'&&s[i+3]=='v'&&s[i+4]=='a')
            {
                cc++;
            }
            if(s[i]=='A'&&s[i+1]=='n'&&s[i+2]=='n')
            {
                dd++;
            }
            if(s[i]=='N'&&s[i+1]=='i'&&s[i+2]=='k'&&s[i+3]=='i'&&s[i+4]=='t'&&s[i+5]=='a')
            {
                ee++;
            }

        }
        sum=aa+bb+cc+dd+ee;
        if(sum==1)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}

第二种:

首先了解一下C++ substr()函数,两种使用方式:


假设:string s = "0123456789";

string sub1 = s.substr(5); //只有一个数字5表示从下标为5开始一直到结尾:sub1 = "56789"

string sub2 = s.substr(5, 3); //从下标为5开始截取长度为3位:sub2 = "567"

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <map>
#include <cstring>
using namespace std;
typedef long long LL;
string str[] = { "Danil", "Olya", "Slava", "Ann", "Nikita" };
string S;
int main()
{
    cin >> S;
    int flag = 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])//依次遍历字符串S,找到该串j位置到str[i]长度位置中符合str[i]的字符串
            {
                flag++;
            }
        }
    }
    if (flag == 1)
    {
        cout<<"YES"<<endl;
    }
    else
    {
        cout<<"NO"<<endl;
    }
    return 0;
}

第三种方法:

了解一下C++ find()和rfind()函数

 string str ("The sixth sick sheik's sixth sheep's sick.");
    string key ("sixth");
    int a=str.find(key);//返回左边子串的第一个元素的位置4
    int b=str.rfind(key);//返回右边子串的第一个元素的位置23
    cout<<a<<" "<<b<<endl;

输出情况: a=4;  b=23;

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <map>
#include <cstring>
using namespace std;
typedef long long LL;
string str[] = { "Danil", "Olya", "Slava", "Ann", "Nikita" };
string S;
int main()
{

	string str[5] = {"Danil", "Olya", "Slava", "Ann","Nikita" };
	string ss;
	cin >> ss;
	int cnt = 0;
	bool isok = false;
	for (int i = 0; i < 5; ++i) {
		int p = ss.find(str[i]);
		int q = ss.rfind(str[i]);
		if (p == -1 || q == -1)	continue;//不含有这个朋友的名字
		if (p == q)	isok = true;//表明只含一个这个字符串
		cnt++;//记录含有多少个朋友的名字
	}
	if (cnt == 1 && isok)
		cout << "YES" << endl;
	else
		cout << "NO" << endl;
    return 0;
}

                

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值