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
对于所有人名 只能有一个出现,并且那个出现的人名只能出现一次
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <deque>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define eps 1e-8
#define PI acos(-1)
#define INF 0x3f3f3f3f
using namespace std;
const int N=2000 + 10 ;
typedef long long int LL;
const int dir[4][2]= { {1,0},{0,1},{-1,0},{0,-1} };
int GCD(int a,int b)
{
return b ? GCD(b,a%b) : a;
}
int main()
{
int i,j;
char fri[5][10]= {"Danil","Olya","Slava","Ann","Nikita"};
char sam[20000]= {0};
gets(sam);
int ha[6]= {0};
int clen=0,flag=0;
clen=strlen(sam);
for(i=0; sam[i]; i++)
{
for(j=0; j<5; j++)
{
int len=0;
len=strlen(fri[j]);
if( !(i+len>clen) )
{
if(strncmp(&sam[i],fri[j],len)==0)
{
flag++;
}
}
}
}
if(flag==1)
printf("YES\n");
else
printf("NO\n");
return 0;
}
错误代码:
我写的hash[]数组没有判断出 有且仅有 一个人名存在
hash记录了每个人名出现的次数
要想判断出有且仅有,需要
hash[i]==1 && hash[j]!=1 对于任意 j!=i
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <deque>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define eps 1e-8
#define PI acos(-1)
#define INF 0x3f3f3f3f
using namespace std;
const int N=2000 + 10 ;
typedef long long int LL;
const int dir[4][2]= { {1,0},{0,1},{-1,0},{0,-1} };
int GCD(int a,int b)
{
return b ? GCD(b,a%b) : a;
}
int main()
{
int i,j;
char fri[5][10]= {"Danil","Olya","Slava","Ann","Nikita"};
char sam[20000]= {0};
gets(sam);
int ha[6]= {0};
int clen=0;
clen=strlen(sam);
for(i=0; sam[i]; i++)
{
for(j=0; j<5; j++)
{
int len=0;
len=strlen(fri[j]);
if( !(i+len>clen) )
{
if(strncmp(&sam[i],fri[j],len)==0)
{
ha[j]++;
}
}
}
}
int cou=0;
for(i=0; i<5; i++)
{
if(ha[i]==1)
cou++;
}
if(cou==1)
printf("YES\n");
else
printf("NO\n");
return 0;
}