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 ...
随机推荐
- 实际上ECMAScript中并没有对类的定义
首先,我们用一个经典例子来简单阐述一下ECMAScript中的继承机制. 在几何学上,实质上几何形状只有两种,即椭圆形(是圆形的)和多边形(具有一定数量的边).圆是椭圆的一种,它只有一个焦点.三角形. ...
- [poj 2796]单调栈
题目链接:http://poj.org/problem?id=2796 单调栈可以O(n)得到以每个位置为最小值,向左右最多扩展到哪里. #include<cstdio> #include ...
- C ------ 标准函数介绍
sprintf() 函数原型:int sprintf( char *buffer, const char *format [, argument] ... ); 功能介绍: 1.把一个字符串赋值(拷贝 ...
- hive连接数
使用hive分析日志作业很多的时候,需要修改mysql的默认连接数 修改方法 打开/etc/my.cnf文件 在[mysqld] 中添加 max_connections=1000 重启mysql ...
- SpringMVC学习 -- IDEA 创建 HelloWorld
SpringMVC 概述: Spring 为展现层提供的基于 MVC 实际理念的优秀 Web 框架 , 是目前最主流的 MVC 框架之一. 自 Spring3.0 发布及 2013 年 Struts ...
- es6+最佳入门实践(9)
9.Iterator和for...of 9.1.Iterator是什么? Iterator又叫做迭代器,它是一种接口,为各种不同的数据结构提供统一的访问机制.这里说的接口可以形象的理解为USB接口,有 ...
- 我们用CloudStack做什么
原文地址:http://www.sdfengxi.com/?p=376 我想很多同学会有类似的疑问,就是我配置好了CloudStack或者OpenStack之类的环境之后能够提供什么服务或者应用呢?下 ...
- 【Foreign】字符串匹配 [KMP]
字符串匹配 Time Limit: 10 Sec Memory Limit: 256 MB Description Input Output Sample Input 3 3 6 3 1 2 1 2 ...
- #error#错误原因:Cannot find executable for CFBundle 0x8ad60b0 (not loaded)
#error#错误原因:Cannot find executable for CFBundle 0x8ad60b0 </Applications/Xcode.app/Contents/Devel ...
- Apache多网站虚拟目录域名
一台服务器安装了Apache,如何绑定多个域名或网站呢? 最简单高效的方法如下: 1. 先打开Apache的配置文件httpd.conf,在这个文件, 找到这句:“#Include etc/extra ...