A - Alex and broken contest

A - Alex and broken contest

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
[题目链接]https://vjudge.net/contest/291644#problem/A


思路:

题意:这道题就是判断一个字符串中有是否含有目标字符串(5个),以及含有几个目标字符串。如果还有一个目标字符串,就输出YES,否则输出NO。
我用的方法是两个for循环,外层循环是遍历输入的函数,遍历一个字符就判断一下是否符合目标函数的第一个字母,如果符合,就进入内层循环,把字符串对应目标字符串的字符数量赋值给temp,比较temp和目标字符串是否一致,并计算字符串中目标字符串有几个,最后只取包含一个目标字符串的字符串。

AC代码:

#include<stdio.h>
#include<string.h>
char str[10005];
char temp[10005]; 
char str1[6]="Danil",str2[5]="Olya",str3[6]="Slava",
	str4[4]="Ann",str5[7]="Nikita";
int main(){
	while(~scanf("%s",str)){
		int sum=0;
		for(int i = 0 ; str[i] != '\0' ; i++){
			if(str[i]=='D'){
				for(int j = i ; j <= i+4; j++){
					temp[j-i]=str[j];
				}
				temp[5]='\0';//注意加上\0,字符串结束的标志
				//比较字符串是否相等,相等的话返回0
				if(!strcmp(temp,str1)){
					sum+=1;
					i=i+4;
				}
			}
			else if(str[i]=='O'){
				strncpy(temp,str,i);
				for(int j = i ; j <= i+3; j++){
					temp[j-i]=str[j];
				}
				temp[4]='\0';
				if(strcmp(temp,str2)==0){
					sum+=1;
					i=i+3;
				}
			}
			else if(str[i]=='S'){
				strncpy(temp,str,i);
				for(int j = i ; j <= i+4; j++){
					temp[j-i]=str[j];
				}
				temp[5]='\0';
				if(!strcmp(temp,str3)){
					sum+=1;
					i=i+4;
				}
			}
			else if(str[i]=='A'){
				strncpy(temp,str,i);
				for(int j = i ; j <= i+2; j++){
					temp[j-i]=str[j];
				}
				temp[3]='\0';
//				printf("%s\n",temp);
				if(!strcmp(temp,str4)){
					sum+=1;
					i=i+2;
				}
			}
			else if(str[i]=='N'){
				strncpy(temp,str,i);
				for(int j = i ; j <= i+5; j++){
					temp[j-i]=str[j];
				}
				temp[6]='\0';
				if(!strcmp(temp,str5)){
					sum+=1;
					i=i+5;
				}
			}
		}
		if(sum==1)	printf("YES\n");
		else	printf("NO\n");
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值