CodeForces 672B Different is Good
链接:http://codeforces.com/problemset/problem/672/B
本文链接:http://www.cnblogs.com/Ash-ly/p/5491176.html
题意:
输入第一行有一个数字N,紧接着第二行给你一个含有N个字符(全是小写)的字符串,为了使得这个字符串的所有连续的子串都不一样,让你给出最少的变化次数.
思路:
栗aba,连续的子串为a,b,a,ab,ba,含有相等的子串,最少改变一个字符就可以使得所有子串不一样(栗'a'-->'c').对于一个含有N个字符的字符串,暂且不用去考虑长度为2的子串,思考长度为1的子串,要想让这些长度为1的子串都不同,那么换种说法,就是至少改变几个字母使得所给字符串的每个字符都不相同,可以想到当N>26时显然是不可能的,对于当N<27时,要想子串都不同,那么这个字符串应该含有N个不同的字符.所以统计出现的字符个数cnt,用N-cnt即是所需的.
代码:
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <cstdio>
#include <string> int main(){
//freopen("input.txt", "r", stdin);
char s[] = {};
int n;
scanf("%d%s", &n, s);
if(n > ) printf("-1\n");
else{
int aa[] = {}, cnt = ;
for(int i = ; i < n; i++)aa[s[i] - 'a']++;
for(int i = ; i < ; i++)if(aa[i])cnt++;
printf("%d\n", n - cnt);
}
return ;
}
CodeForces 672B Different is Good的更多相关文章
- 【CodeForces 672B】Different is Good
题 字符串所有子串要不同.求修改最少多少个字符. 因为只能是26个字母,显然大于26的不可能有答案,其它情况ans+=u[i]-1:u[i]是字母出现的次数. #include<cstdio&g ...
- codeforces 672B B. Different is Good(水题)
题目链接: B. Different is Good time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
随机推荐
- mac 倍速播放
Mac的倍速播放,使用工具播放是quick time player,使用方式是,按住 “option” + “>>” 即可调整倍数:
- [洛谷P4782]【模板】2-SAT 问题
题目大意:有$n$个布尔变量 $x_1 \sim x_n$,另有$m$个需要满足的条件,每个条件的形式都是"$x_i$ 为$true/false$或$x_j$为$true/false$&qu ...
- [AHOI2013]作业 & Gty的二逼妹子序列 莫队
---题面--- 题解: 题目要求统计一个区间内数值在[a, b]内的数的个数和种数,而这个是可以用树状数组统计出来的,所以可以考虑莫队. 考虑区间[l, r]转移到[l, r + 1],那么对于维护 ...
- Android:Google出品的序列化神器Protocol Buffer使用攻略
习惯用 Json.XML 数据存储格式的你们,相信大多都没听过Protocol Buffer Protocol Buffer 其实 是 Google出品的一种轻量 & 高效的结构化数据存储格式 ...
- [bzoj] 2038 小Z的袜子(hose) || 莫队
原题 给出一个序列,求给定[l,r]内有任意取两个数,有多大概率是一样的 简单的莫队,每次+-当前区间里有的这个颜色的袜子的个数,最后除以(r-l+1)*(r-l)/2即可. 记得约分. #inclu ...
- [Leetcode] word break 拆分词语
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- Small things are better
Yesterday I had fun time repairing 1.5Tb ext3 partition, containing many millions of files. Of cours ...
- 7月17号day9总结
今天学习过程和小结 今天学习了如何使用idea操作hdfs. public class HDFSTest { Configuration configuration; FileSystem ...
- Java输入输出流备忘
重要博客: http://blog.csdn.net/hguisu/article/details/7418161 File dir = new File("\\root"); ...
- 【Foreign】朗格拉日计数 [暴力]
朗格拉日计算 Time Limit: 10 Sec Memory Limit: 128 MB Description Input Output 仅一行一个整数表示答案. Sample Input 5 ...