天天看點

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;
}