您的位置:首页 > 其它

codeforces - 877A - Alex and broken contest【string的一些技巧】

2017-10-28 10:41 507 查看

A. Alex and broken contest

time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard 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

Alex_and_broken_contest

output

NO

input

NikitaAndString

output

YES

input

Danil_and_Olya

output

题意: 给你一个字符串,然后提前给你了五个名字,让你判断这个字符串里面有且仅有一个”提前预设的名字”

分析: 这题很简单,直接暴力就行,当时我是利用string里的find函数,很简单,后来又学会了substr函数的一些技巧,分享下

参考代码(find)

#include <bits/stdc++.h>
using namespace std;

vector<string> s;
int main() {
s.push_back("Danil");
s.push_back("Olya");
s.push_back("Slava");
s.push_back("Ann");
s.push_back("Nikita");
string a;cin>>a;
int res = 0;
for(int i = 0;i < 5;i++) {
if(a.find(s[i]) != a.npos) {
res++;
if(a.rfind(s[i]) != a.find(s[i])) {
res++;
}
}
}
if(res == 1) cout<<"YES"<<endl;
else cout<<"NO"<<endl;

return 0;
}


参考代码(substr)

#include<bits/stdc++.h>
using namespace std;

string s[6] = {"Danil","Olya","Slava","Ann","Nikita"};
int main(){
ios_base::sync_with_stdio(0);
string a;cin>>a;
int res = 0;
for(int i = 0;i < 5;i++) {
for(int j = 0;j+s[i].size() <= a.size();j++) {
string t = a.substr(j,s[i].size());
if(t == s[i]) {
res++;
}
}
}
if(res == 1) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
return 0;
}


如有错误或遗漏,请私聊下UP,thx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: