CF1660C Get an Even String(贪心)
文章目录
- 1.描述:
- 2. 输入:
- 3.输出:
- 4.样例输入:
- 5.样例输出:
- 6.题目大意:
- 7.思路
- 8.代码
- 9.反思
1.描述:
A string a=a1a2…an is called even if it consists of a concatenation (joining) of strings of length 2 consisting of the same characters. In other words, a string a is even if two conditions are satisfied at the same time:
一个字符串a是偶数串当它由相同字符组成的长度为2的字符串串联(连接)而成,换句话说,一个字符串是偶数字符串如果同时满足以下两个条件
1.its length n is even;
1.它的长度 n 是偶数
2.for all odd i (1≤i≤n−1), ai=ai+1 is satisfied.
2.对于所有的奇数字母 i 满足 ai = ai + 1;
For example, the following strings are even: “” (empty string), “tt”, “aabb”, “oooo”, and “ttrrrroouuuuuuuukk”. The following strings are not even: “aaa”, “abab” and “abba”.
例如:接下来的字符串是偶数字符串," "(空串),“tt”, “aabb”, “oooo”, and “ttrrrroouuuuuuuukk”.接下来的字符串不是偶数字符串,“aaa”, “abab” and “abba”.
Given a string s consisting of lowercase Latin letters. Find the minimum number of characters to remove from the string s to make it even. The deleted characters do not have to be consecutive.
给出一个包含小写拉丁字母的字符串,找到需要把字符串变成偶数字符串需要移除的最小字母数,删除的字符不必是连续的。
2. 输入:
The first line of input data contains an integer t (1≤t≤104) —the number of test cases in the test.
The descriptions of the test cases follow.
Each test case consists of one string s (1≤|s|≤2⋅105), where |s| — the length of the string s. The string consists of lowercase Latin letters.
It is guaranteed that the sum of |s| on all test cases does not exceed 2⋅105.
3.输出:
For each test case, print a single number — the minimum number of characters that must be removed to make s even.
4.样例输入:
6
aabbdabdccc
zyx
aaababbb
aabbcc
oaoaaaoo
bmefbmuyw
5.样例输出:
3
3
2
0
2
7
6.题目大意:
给出一个字符串,要把它变成一个偶数字符串。
7.思路
用贪心的思想,从左往右找,每找到一对可配对的就进配对,然后把前边未配对的都删掉
例如:
oaoaaaoo
位置1的o和位置3的o配对,位置2的a被删去,
位置4的a和位置5的a配对
位置7的o和位置8的o配对,位置6的a被删去
我们可以简单证明一下贪心策略的正确性:
1.adda
首先是这种要配对的元素之间有配对好的元素的情况,这种情况按照贪心策略就是删去dd,让aa配对,删去两个元素,我们如果让dd配对,那就要删去aa,删去元素个数相同,不影响结果。
同理
adcffttcda这个串,无论留下那一对偶数串最后的结果和贪心结果(删去个数)都是相同的
2.adad
其次考虑一下这种配对删去元素对后续配对有影响的情况,a和a配对删去d会对下一次d配对产生影响,但是容易看出这个串无论怎么配对其处理结果和贪心结果(删去个数)也都是相同的。
3,aspca
最后是这种最简单的情况,就是配对元素之间的元素无法跟任何元素配对,直接删去就好。
故经过我们简单的证明,可以用贪心来做
8.代码
#include<bits/stdc++.h>
using namespace std;
map<char,int>mp;
string s;int n;int main()
{cin>>n;for(int i=1;i<=n;i++){cin>>s;int len=s.size(),cnt=len;for(int i=0;i<len;i++){mp[s[i]]++;//计数if(mp[s[i]]==2){cnt-=2;//有一个字母配对成功,减去这个字母的长度mp.clear(); //清空前面字母的影响} }mp.clear(); //清空mapcout<<cnt<<endl;//所有字母减去可配对的就是要删去的}return 0;
}
9.反思
注意这种贪心策略的写法,这种模拟的写法非常值得学习
CF1660C Get an Even String(贪心)相关推荐
- UVa1368 - DNA Consensus String(贪心算法)
问题:给出n个字符串,由A,T,G,C字符组成.其中两个字符串的hamiton距离为为相同对应位置不同字符的个数.要求求出一个字符串,其hamiton距离最短. 思路:在遍历n个字符串时,取其对应位置 ...
- CF1660C Get an Even String
// 从前往后扫,用一个数组记录字母出现次数. // 如果当前字母出现两次,那么说明这个字母对可以 最快匹配, // cnt+=2; // 此时清空数组(其他字母肯定没有用了,否则不满足题目要求), ...
- 1506G. Maximize the Remaining String
G. Maximize the Remaining String 贪心,放置时,如果前面一个小比他小,并且后面还有,那么就把前面的删除 #include <bits/stdc++.h> u ...
- (Greedy approach)Find longest word in dictionary that is a subsequence of a given string
Find longest word in dictionary that is a subsequence of a given string 贪心算法: 1)将D按字符串的长度,从长到短排序. 2) ...
- 【Java 数据结构 算法】宁可累死自己, 也要卷死别人 18 贪心算法
[Java 数据结构 & 算法]⚠️宁可累死自己, 也要卷死别人 18⚠️ 贪心算法 概述 贪心算法 电台覆盖问题 代码实现 概述 从今天开始, 小白我将带大家开启 Java 数据结构 &am ...
- 训练指南第一部分解题报告
主要是提供训练指南第一部分解题报告链接,后面会持续更新中 307 - Sticks (DFS+剪枝) 11292 - Dragon of Loowater (贪心) 11729 - Commando ...
- Codeforces 题目合集+分类+代码 【Updating...】【361 in total】
961A - Tetris 模拟 ...
- 暑假N天乐【比赛篇】 —— 2019牛客暑期多校训练营(第三场)
这场相对来说友好一点,本来前几天就补差不多了,然后忘记发了... 以下题解包括:\(A \ \ \ B \ \ \ F \ \ \ G \ \ \ H \ \ \ J\) \(D\) 题队友补了,我也 ...
- window下面如何使用swoole
Cygwin网址:http://www.cygwin.com/ 安装Cygwin: 点击exe安装文件 点击下一步 选择install from internet点击下一步 自定义安装目录,不要按在系 ...
最新文章
- 关于ContinuationFilter的使用
- 数据库存在即更新的并发处理 - 转
- mysql 集群与主从_Mysql集群和主从
- 【OpenCV 例程200篇】40. 图像分段线性灰度变换
- 电脑桌面点任何文件都打开计算机,小编教你电脑开机自动打开文件夹怎么解决...
- PAT (Basic Level) Practice1008 数组元素循环右移问题
- webrtc在ubuntu14.04上的编译过程(12.04亦可)
- 【408考研】数据结构 —— 第一章 绪论
- java-net-php-python-jsp刺绣作品展示网站计算机毕业设计程序
- RK3399平台开发系列讲解(USB设备驱动)5.31、使用usb gadget configfs配置USB功能
- 皮卡丘(pikachu)暴力破解
- Rockchip RK3588 kernel dts解析之系统休眠配置rockchip_suspend
- 基于微信小程序的图书馆管理系统设计与实现(论文+程序设计源码+数据库文件)
- HTML5基础标签学习
- Pod环境变量和initContainer
- python 创建目录时间_python实现根据当前时间创建目录并输出日志
- 期待已久的《刀剑神域》第三季终于来了
- CT值(亨氏值H) 孔隙率 像素点 灰度值 RGB
- Python小作业 列举红黄绿小球的组合
- Kotlin协程 - - - 协程的简单使用