C. Get an Even String题解(dp)
A字符串a=a1a2…。即使由由相同字符组成的长度为2的字符串的串联(连接)组成,也会调用。换句话说,即使同时满足两个条件,字符串a也是:
其长度n为偶数;
对于所有的奇数i(1≤i≤n−1),ai=ai+1是满足的。
例如,以下字符串是偶数:“”(空字符串)、“TT”、“AABB”、“oooo”和“ttrrrroouuuuuuuukk”。以下字符串不是偶数:“aaa”、“abab”和“abba”。
给定由小写拉丁字母组成的字符串s。找到要从字符串%s中删除以使其相等的最小字符数。删除的字符不必是连续的。
输入。
第一行输入数据包含一个整数t(1≤t≤104)–测试中的测试用例数。
测试用例的描述如下。
每个测试用例由一个字符串s(1≤|s|≤2⋅105)组成,其中|s|-字符串s的长度。该字符串由小写拉丁字母组成。
保证所有测试用例上的|s|之和不超过2⋅105。
输出。
对于每个测试用例,打印一个数字–使s成为偶数必须删除的最小字符数。
输入
6
aabbdabdccc
zyx
aaababbb
aabbcc
oaoaaaoo
bmefbmuyw
输出
3
3
2
0
2
7
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:
its length n is even;
for all odd i (1≤i≤n−1), ai=ai+1 is satisfied.
For example, the following strings are even: “” (empty string), “tt”, “aabb”, “oooo”, and “ttrrrroouuuuuuuukk”. The following strings are not even: “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.
Input
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.
Output
For each test case, print a single number — the minimum number of characters that must be removed to make s even.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;public class Main {public static void main(String[] args) throws IOException {InputStreamReader input=new InputStreamReader(System.in);BufferedReader reader=new BufferedReader(input);int t=Integer.parseInt(reader.readLine());for (int i=0;i<t;i++){String ss= reader.readLine();ArrayList list=new ArrayList();int sum=0;int len=ss.length();for (int j=0;j<len;j++){if (list.contains(ss.charAt(j)))//判断list里是否有该字符,有则配对上,在list中减去该配对的字符,并清空list,其他字符都被sum计数,因为sum只减了一(该匹配的字符){sum--;list=new ArrayList();}else{sum++;list.add(ss.charAt(j));}}System.out.println(sum);}input.close();}
}
C. Get an Even String题解(dp)相关推荐
- LA 3363 String Compression(dp)
题意:给出一个字符串,再给出一个字符串压缩的方法,问该字符串最少能压缩到多少位. 思路:一直没思路啊,最后只能看题解,看了题解发现这题并没有想象中的难--用dp[i][j]表示区间i~j的最小长度,则 ...
- 【HDU - 4055】Number String(dp,思维)
题干: The signature of a permutation is a string that is computed as follows: for each pair of consecu ...
- *【HDU - 5707】Combine String(dp)
题干: Given three strings aa, bb and cc, your mission is to check whether cc is the combine string of ...
- Codeforces 833B 题解(DP+线段树)
题面 传送门:http://codeforces.com/problemset/problem/833/B B. The Bakery time limit per test2.5 seconds m ...
- PAT甲级1093 Count PAT‘s :[C++题解]DP、状态机模型dp
文章目录 题目分析 题目链接 题目分析 来源:acwing 分析:统计子串"PAT"的数量. 状态机模型:本题需要的是PAT,需要选3个字母,对应三条边,需要4个状态. 下面以样例 ...
- PAT甲级1068 Find More Coins (30 分):[C++题解]DP、背包问题、dp输出方案
文章目录 题目分析 题目链接 题目分析 来源:acwing 分析:m是背包容量,a1,a2,....,ana_1,a_2,....,a_na1,a2,....,an是n个物品,第i个物品的体积是 ...
- PAT甲级1007 Maximum Subsequence Sum :[C++题解]DP,最大子序列和、求最优的区间方案
文章目录 题目分析 题目链接 题目分析 来源:acwing 分析: dp题. 这道题糅合了两个知识点: dp求最值(区间之和) 动态求区间方案:区间之和相等的条件下:要求区间左端点最靠前,如果左端点相 ...
- 买股票的最佳时机(六种题解dp)
引言 买股票的最佳时机类的题目也是很经典的动态规划题目,出题人通过各种花里胡哨的买股票方法来考察(虐待)你,下面我们就开始看看一类的题目的各种花样: 买股票的最佳时机 给定一个数组 prices ,它 ...
- [HDU3336]Count the string(KMP+DP)
Solution 不稳定的传送门 对KMP的灵活应用 设dp[i]表示前[1,i]的答案 那么dp[i]=dp[p[i]]+1,p[i]为失配函数 Code #include <cstdio&g ...
- HDU4055 - number string(DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4055 思路:dp[i][j]表示处理前i个字符以j结尾可能的序列数. 当a[i]=='I'时,dp[i ...
最新文章
- shell 流程控制语句
- mysql 集群切换_完美起航-MySQLMHA高可用集群部署及故障切换(图文详解)
- linux c主要应用在哪个领域
- Winform中对ZedGraph的曲线标签进行设置,比如去掉标签边框
- Input为number类型maxlength不好使,用js轻松解决
- ChemBioDraw 制作DMT屏保
- 列表页的动态条件搜索
- 信息化基础建设 工作流开发
- 列车控制matlab仿真,基于matlab的列车纵向碰撞建模仿真研究
- iPhone app 和android app开发环境和语言的区别
- 【可视化】数据仓库与数据挖掘大作业
- 基于三菱PLC的两轴圆弧插补
- 【报告分享】 2020国人旅游出行研究报告-企鹅智库(附下载)
- 如何用决策树模型做数据分析?
- Alook浏览器获取Cookie教程
- 信息系统项目管理师-3项目立项管理
- Python networkx 根据节点坐标来画网络图
- 高新企业申请补贴需要什么条件
- UML建模与软件开发设计(六)——类图设计与类之间的关系
- 有哪些你看过五遍以上的电影?