Codeforces Round #442 (Div. 2) A B

简介: A. Alex and broken contest time limit per test2 seconds memory limit per test256 megaby...

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
NO

You need just implement what is written in the statements. Count the total number of entries of the names and check if it’s equal to 1.

开始没有注意同一名字出现多次的情况

#include <iostream>
#include <string>
using namespace std;
typedef long long LL;
int main(){
    int ans=0;
    string s;
    cin>>s;
    for(int i=0;i<s.size();i++){
        if(s.substr(i,5)=="Danil")ans++;
        if(s.substr(i,4)=="Olya")ans++;
        if(s.substr(i,5)=="Slava")ans++;
        if(s.substr(i,3)=="Ann")ans++;
        if(s.substr(i,6)=="Nikita")ans++;
    }
     if(ans==1) cout<<"YES"<<endl;
     else cout<<"NO"<<endl;
     return 0;
}

B. Nikita and string
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
One day Nikita found the string containing letters “a” and “b” only.

Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters “a” and the 2-nd contains only letters “b”.

Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get?

Input
The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters “a” and “b”.

Output
Print a single integer — the maximum possible size of beautiful string Nikita can get.

Examples
input
abba
output
4
input
bab
output
2
Note
It the first sample the string is already beautiful.
In the second sample he needs to delete one of “b” to make it beautiful.

Let prefa[i] be the count of letter “a” in prefix of length i and prefb[i] be the count of letter “b” in prefix of length i.
Let’s fix two positions i and j, 1 ≤ i ≤ j ≤ n, so we remove all “b” from prefix, which ends in i, and suffix, which starts in j, and all “a” between positions i and j. Then length of string is (prefa[n] - prefa[j]) + (prefb[j] - prefb[i]) + (prefa[i]).
Using two for loops we find optimal i and j and calculate answer.

#include <iostream>
#include <string>
using namespace std;
typedef long long LL;
const int maxn=5005;
int suma[maxn],sumb[maxn];
int main() {
    string str;
    cin>>str;
    suma[0]=sumb[0]=0;
    for(int i=1;i<=str.size();i++){
        suma[i]=suma[i-1];
        sumb[i]=sumb[i-1];
        if(str[i-1]=='a'){
            suma[i]++;
        }else {
            sumb[i]++;
        }
    }
    int n = str.size();
    int ans = 0;
    for(int i=0;i<n;i++){
        for(int j=i;j<=n;j++){
            int sum = sumb[j]-sumb[i]+suma[i]+suma[n]-suma[j];
            ans = max(sum , ans);
        }
    }
    cout<<ans<<endl;
    return 0;
}
目录
相关文章
|
1月前
|
人工智能 测试技术 芯片
Codeforces Round 963 (Div. 2)
Codeforces Round 963 (Div. 2)
|
人工智能 算法 BI
Codeforces Round 891 (Div. 3)
Codeforces Round 891 (Div. 3)
120 0
Codeforces Round 891 (Div. 3)
Codeforces Round #178 (Div. 2)
在n条电线上有不同数量的鸟, Shaass开了m枪,每一枪打的是第xi条电线上的第yi只鸟,然后被打中的这只鸟左边的飞到第i-1条电线上,右边的飞到i+1条电线上,没有落脚点的鸟会飞走。
51 0
|
人工智能 算法 BI
Codeforces Round #179 (Div. 2)A、B、C、D
我们每次加进来的点相当于k,首先需要进行一个双重循环找到k点和所有点之间的最短路径;然后就以k点位判断节点更新之前的k-1个点,时间复杂度降到O(n^3),而暴力解法每次都要进行floyd,时间复杂度为O(n^4);相比之下前述解法考虑到了floyd算法的性质,更好了运用了算法的内质。
54 0
Codeforces Round #192 (Div. 2) (329A)C.Purification
Codeforces Round #192 (Div. 2) (329A)C.Purification
45 0
Codeforces Round #742 (Div. 2)
Codeforces Round #742 (Div. 2)
47 0
|
人工智能
Codeforces Round #723 (Div. 2)B. I Hate 1111
Description You are given an integer x. Can you make x by summing up some number of 11,111,1111,11111,…? (You can use any number among them any number of times). For instance, 33=11+11+11 144=111+11+11+11
178 0
Codeforces Round #723 (Div. 2)B. I Hate 1111
Equidistant Vertices-树型dp-Codeforces Round #734 (Div. 3)
Description A tree is an undirected connected graph without cycles. You are given a tree of n vertices. Find the number of ways to choose exactly k vertices in this tree (i. e. a k-element subset of vertices) so that all pairwise distances between the selected vertices are equal (in other words,
139 0