CodeForces 877A Alex and broken contest KMP入门

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

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
Copy
Alex_and_broken_contest
output
Copy
NO
input
Copy
NikitaAndString
output
Copy
YES
input
Copy
Danil_and_Olya
output
Copy
NO

关键是查询是否有只有一个人的名字出现&&这个人的名字只出现一次。

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;

void compute_prefix(int *next,char *p)
{
	int n,k;
	n=strlen(p);
	next[0]=next[1]=0;
	k=0;
	for(int i=2;i<=n;i++)
	{
		while(k&&p[k]!=p[i-1])
		k=next[k];
		if(p[k]==p[i-1])
		k++;
		next[i]=k;
	}
}

int kmp_match(char *text,char *p,int *next)
{
	int ans=0,j=0;
	int text_len=strlen(text);
	int p_len=strlen(p);
	for(int i=0;i<text_len;i++)
	{
		while(j&&text[i]!=p[j])
		j=next[j];
		if(text[i]==p[j])
		j++;
		if(j==p_len)
		ans++;
	}
	return ans;
}






char s[200];
int next[200];
int main()
{
	
	while(scanf("%s",s)!=EOF)
	{
		char name[5][10]={"Danil", "Olya", "Slava", "Ann" , "Nikita"};
		int flag=0;
		for(int i=0;i<5;i++)
		{
			flag+=kmp_match(s,name[i],next);
	
		}
		if(flag==1)
		printf("YES\n");
		else
		printf("NO\n");
	}
	
	return 0;
}


猜你喜欢

转载自blog.csdn.net/cao2219600/article/details/80159732
今日推荐