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;
}
目录
相关文章
|
9月前
|
弹性计算 缓存 前端开发
阿里云服务器ECS u1、c7、e实例、c8i实例有什么区别?性能有差异吗?
阿里云ECS实例包括经济型e、通用算力型u1、计算型c7和c8i,性能与价格各异。经济型e为共享型,适合轻量应用;u1性价比高,适配中小型企业需求;c7和c8i为企业级独享型,性能依次递增,适用于高性能场景。以2核4G为例,u1实例199元/年起,带5M带宽;c7和c8i价格更高但性能更强。选择时需根据实际需求权衡性能与成本。
401 0
|
9月前
|
机器学习/深度学习 算法 调度
【强化学习】基于深度强化学习的微能源网能量管理与优化策略研究【Python】
本项目基于深度Q网络(DQN)算法,通过学习预测负荷、可再生能源输出及分时电价等信息,实现微能源网的能量管理与优化。程序以能量总线模型为基础,结合强化学习理论,采用Python编写,注释清晰,复现效果佳。内容涵盖微能源网系统组成、Q学习算法原理及其实现,并提供训练奖励曲线、发电单元功率、电网交互功率和蓄电池调度等运行结果图表,便于对照文献学习与应用。
|
12月前
|
存储 算法 测试技术
当leetcode真题上了生产引发的线上问题
11月7日上午,支付网关下游HSF请求出现失败,一台额度中心服务器异常。经排查,发现是B算法在处理47笔订单时导致内存溢出(OOM)。该算法用于计算用户可用额度下的最优订单组合,但因递归创建链表占用过多内存而崩溃。为解决此问题,团队紧急将用户流量切换至A算法,并对B算法进行优化。通过分治+回溯和背包算法的对比实验,最终选择根据订单数和金额阈值动态选择算法,确保系统稳定性和性能。此次事件提醒我们,在编程中需充分考虑边界情况并进行性能测试,避免极端情况对系统的影响。
|
存储 安全 Java
Java修仙之路,十万字吐血整理全网最完整Java学习笔记(基础篇)
从Java环境的搭建到实际代码的编写,从基本用法的讲解到底层原理的剖析,深度解析Java基础知识。本文是《Java学习路线》专栏的起始文章,旨在提供一套完整的Java学习路线,覆盖Java基础知识、数据库、SSM/SpringBoot等框架、Redis/MQ等中间件、设计模式、架构设计、性能调优、源码解读、核心面试题等全面的知识点,并在未来不断更新和完善,帮助Java从业者在更短的时间内成长为高级开发。
Java修仙之路,十万字吐血整理全网最完整Java学习笔记(基础篇)
|
SQL 存储 前端开发
【java】树形结构分页(真分页)
【java】树形结构分页(真分页)
428 1
|
人工智能 机器人 vr&ar
Midjourney高效使用技巧总结(二)
这篇文章总结了Midjourney AI绘画工具的高效使用技巧,包括常用指令/参数、实操案例和参考网站,帮助用户更好地掌握如何使用Midjourney进行艺术创作。
Midjourney高效使用技巧总结(二)
|
缓存 监控 前端开发
多个异步操作对网页性能的影响及优化建议
多个异步操作会影响网页性能,主要体现在网络请求延迟、资源竞争及浏览器限制等方面,可能导致页面加载缓慢。为优化性能,可采用 `Promise.all()` 并行处理、请求合并、懒加载、缓存利用、CDN 托管、请求优化及性能监控等策略,从而提升用户体验。
|
Java API 开发者
|
数据采集 Web App开发 JavaScript
快速参考:用C# Selenium实现浏览器窗口缩放的步骤
在C#结合Selenium的网络爬虫应用中,掌握浏览器窗口缩放、代理IP、cookie与user-agent设置至关重要。本文详述了如何配置代理(如亿牛云加强版),自定义用户代理,启动ChromeDriver,并访问目标网站如抖音。通过执行JavaScript代码实现页面缩放至75%,并添加cookie增强匿名性。此策略有效规避反爬机制,提升数据抓取的准确度与范围。代码示例展示了整个流程,确保爬虫操作的灵活性与高效性。
366 3
|
自然语言处理 关系型数据库 数据管理
阿里云百炼|析言GBI产品入门指导
析言基础入门的操作体验,带领大家一起探索阿里云百炼|析言GBI的奥秘。
4432 19