第五次个人赛
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
问题:给一个字符串s,然后找出字符串里面仅出现一次的 Alex朋友的姓名,超过两次或没有输出NO,出现一次输出YES
三种方法:
第一种简单暴力:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <map>
#include <cstring>
using namespace std;
typedef long long LL;
int main()
{
string a="Danil",b="Olya",c="Slava",d="Ann",e="Nikita";
string s;
int sum;
while(cin>>s)
{
sum=0;
int aa=0,bb=0,cc=0,dd=0,ee=0;
for(int i=0; i<s.size(); i++)
{
if(s[i]=='D'&&s[i+1]=='a'&&s[i+2]=='n'&&s[i+3]=='i'&&s[i+4]=='l')
{
aa++;
}
if(s[i]=='O'&&s[i+1]=='l'&&s[i+2]=='y'&&s[i+3]=='a')
{
bb++;
}
if(s[i]=='S'&&s[i+1]=='l'&&s[i+2]=='a'&&s[i+3]=='v'&&s[i+4]=='a')
{
cc++;
}
if(s[i]=='A'&&s[i+1]=='n'&&s[i+2]=='n')
{
dd++;
}
if(s[i]=='N'&&s[i+1]=='i'&&s[i+2]=='k'&&s[i+3]=='i'&&s[i+4]=='t'&&s[i+5]=='a')
{
ee++;
}
}
sum=aa+bb+cc+dd+ee;
if(sum==1)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
第二种:
首先了解一下C++ substr()函数,两种使用方式:
假设:string s = "0123456789";
string sub1 = s.substr(5); //只有一个数字5表示从下标为5开始一直到结尾:sub1 = "56789"
string sub2 = s.substr(5, 3); //从下标为5开始截取长度为3位:sub2 = "567"
#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;
}
第三种方法:
了解一下C++ find()和rfind()函数
string str ("The sixth sick sheik's sixth sheep's sick.");
string key ("sixth");
int a=str.find(key);//返回左边子串的第一个元素的位置4
int b=str.rfind(key);//返回右边子串的第一个元素的位置23
cout<<a<<" "<<b<<endl;
输出情况: a=4; b=23;
#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()
{
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;
}