Codeforces Round #442 A Alex and broken contest

Ciwikun有5个朋友,他们的名字分别是"Danil", "Olya", "Slava", "Ann"和"Nikita".

这个问题很简单: 给你一个字符串, 问: 字符串中是否只包含Ciwikun的一个朋友的名字,并且这个名字仅出现一次

他们的名字区分大小写

Input

这个字符串只包含小写字母,大写字母和"_"(下划线), 字符串的长度不超过100

Output

如果Ciwikun的朋友的名字只在字符串中仅出现了一次输出"YES",否则输出"NO".

Simple Input 1Alex_and_broken_contestSimple Output 1NOSimple Input 2NikitaAndStringSimple Output 2YESSimple Input 3Danil_and_OlyaSimple Output 3NO

    

代码一:(冗长的代码 也算是暴力出来了 )

#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main()
{
	string s;
//	"Danil";"Olya";"Slava";	"Ann";"Nikita";
	int c1=0,c2=0,c3=0,c4=0,c5=0;
	int flag1=0,flag2=0,flag3=0,flag4=0,flag5=0;
	cin>>s;
	for(int i=0;i<s.length();i++)
	{
		if(s[i]=='D')
		{
			if(s[i+1]=='a'&&s[i+2]=='n'&&s[i+3]=='i'&&s[i+4]=='l')
			{
				flag1=1;
				c1++;
			}
		}
		else if(s[i]=='O')
		{
			if(s[i+1]=='l'&&s[i+2]=='y'&&s[i+3]=='a')
			{
				flag2=1;
				c2++;
			}
		}
		else if(s[i]=='S')
		{
			if(s[i+1]=='l'&&s[i+2]=='a'&&s[i+3]=='v'&&s[i+4]=='a')
			{
				flag3=1;
				c3++;
			}
		}else if(s[i]=='A')
		{
			if(s[i+1]=='n'&&s[i+2]=='n')
			{
				flag4=1;
				c4++;
			}
		}
		else if(s[i]=='N')
		{
			if(s[i+1]=='i'&&s[i+2]=='k'&&s[i+3]=='i'&&s[i+4]=='t'&&s[i+5]=='a')
			{
				flag5=1;
				c5++;
			}
		}
	}
	if(flag1==1&&c1==1&&flag2==0&&flag3==0&&flag4==0&&flag5==0)
	{
		cout<<"YES"<<endl;
	}
	else if(flag2==1&&c2==1&&flag1==0&&flag3==0&&flag4==0&&flag5==0)
	{
		cout<<"YES"<<endl;
	}
	else if(flag3==1&&c3==1&&flag1==0&&flag2==0&&flag4==0&&flag5==0)
	{
		cout<<"YES"<<endl;
	}
	else if(flag4==1&&c4==1&&flag1==0&&flag2==0&&flag3==0&&flag5==0)
	{
		cout<<"YES"<<endl;
	}
	else if(flag5==1&&c5==1&&flag1==0&&flag2==0&&flag3==0&&flag4==0)
	{
		cout<<"YES"<<endl;
	}else cout<<"NO"<<endl;
	return 0;
 } 

代码二:(别人优秀的代码了解一下 )

#include<cstdio>  
#include<iostream>  
#include<string>  
#include<cstring>  
#include<cmath>  
using namespace std;  
  
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])  
            {  
                flag++;  
            }  
        }  
    }  
    if (flag == 1)  
    {  
        puts("Yes\n");  
    }  
    else  
    {  
        puts("No\n");  
    }  
    return 0;  
}  


学长 henuzxy

#include<iostream>
#include<cstring>
#include<cmath>
#include<vector>
#include<string>
#include<algorithm>
#include<set>
using namespace std;
set<int, less<int>> st;

int main(void) {
	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;
}
了解一下


C++ substr用法了解一下

substr有2种用法:
假设:string s = "0123456789";

string sub1 = s.substr(5); //只有一个数字5表示从下标为5开始一直到结尾:sub1 = "56789"

string sub2 = s.substr(5, 3); //从下标为5开始截取长度为3位:sub2 = "567"



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值