codeforces - 877A - Alex and broken contest【string的一些技巧】
2017-10-28 10:41
507 查看
A. Alex and broken contest
time limit per test2 secondsmemory 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_contestoutput
NOinput
NikitaAndStringoutput
YESinput
Danil_and_Olyaoutput
题意: 给你一个字符串,然后提前给你了五个名字,让你判断这个字符串里面有且仅有一个”提前预设的名字”分析: 这题很简单,直接暴力就行,当时我是利用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
相关文章推荐
- Codeforces 877 A Alex and broken contest
- codeforces 442 A. Alex and broken contest(水题)
- 【Codeforces Round #442 (Div. 2) A】Alex and broken contest
- CF#877 A. Alex and broken contest(KMP)
- Codeforces Round #442 (Div. 2) A. Alex and broken contest
- Codeforces Round #442 (Div. 2) A Alex and broken contest
- A. Alex and broken contest
- Codeforces Round #442 (Div. 2) A. Alex and broken contest
- A. Alex and broken contest
- Codeforces 877 B. Nikita and string (技巧)
- Codeforces-258D:Little Elephant and Broken Sorting(概率DP)
- CODEFORCES 272B Dima and Sequence <简单技巧 + 类比奇偶数>
- 关于string.xml的一些技巧
- 解密照片级表现技巧(一些关于UE4建筑表现的废话)(转自UnrealChina.Alex_Tsui)
- Codeforces 890 D. Restoration of string (技巧)
- Codeforces 677E Vanya and Balloons(DP + 一些技巧)
- 【CodeForces】501B - Misha and Changing Handles(STL - string & vector & pair)
- 【CodeForces 624C】Graph and String
- 【非常高%】【codeforces 733A】Grasshopper And the String
- Codeforces - AIM Tech Round (Div. 2)C - Graph and String