Codeforces 877A - Alex and broken contest

A. Alex and broken contest
time limit per test
2 seconds
memory limit per test
256 megabytes
 

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

 思路:第一种直接暴力匹配,第二种利用c++的substr()函数

#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
    string s[5]={"Danil", "Olya", "Slava", "Ann", "Nikita"};
    string ss;
    while(getline(cin, ss))
    {
        int cnt = 0;
        for(int i = 0;i < ss.length();i++)
        {
            for(int j = 0;j < 5;j++)
                if(ss[i] == s[j][0])
                {
                    int len = s[j].length();
                    int tmp = i;
                    bool flag = true; 
                    for(int k = 0;k < len;k++)
                    {
                        if(ss[tmp] != s[j][k])
                            flag = false;
                        tmp++;
                    }
                    if(flag)
                        cnt++;
                }
            if(cnt >= 2)
            {
                printf("NO\n");
                break;
            }
        }
        if(cnt == 1)
            printf("YES\n");
        else if(cnt == 0)
            printf("NO\n");
    }
} 

第二种

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

 

 

posted @ 2020-02-03 15:00  YLzcty  阅读(305)  评论(0)    收藏  举报