CodeForces-672B-Different is Good
2016-05-20 09:44
176 查看
Description
A wise man told Kerem “Different is good” once, so Kerem wants all things in his life to be different.
Kerem recently got a string s consisting of lowercase English letters. Since Kerem likes it when things are different, he wants all substrings of his string s to be distinct. Substring is a string formed by some number of consecutive characters of the string. For example, string “aba” has substrings “” (empty substring), “a”, “b”, “a”, “ab”, “ba”, “aba”.
If string s has at least two equal substrings then Kerem will change characters at some positions to some other lowercase English letters. Changing characters is a very tiring job, so Kerem want to perform as few changes as possible.
Your task is to find the minimum number of changes needed to make all the substrings of the given string distinct, or determine that it is impossible.
Input
The first line of the input contains an integer n (1 ≤ n ≤ 100 000) — the length of the string s.
The second line contains the string s of length n consisting of only lowercase English letters.
Output
If it’s impossible to change the string s such that all its substring are distinct print -1. Otherwise print the minimum required number of changes.
Sample Input
Input
2
aa
Output
1
Input
4
koko
Output
2
Input
5
murat
Output
0
给一个长度为n的字符串,问最少需要改变几个字母才能使这个字符串的字串全不相同
全不相同只能是不同的字母组合而成,故n大于26的绝不可能,小于等于26的只需把大于一的改变就行
A wise man told Kerem “Different is good” once, so Kerem wants all things in his life to be different.
Kerem recently got a string s consisting of lowercase English letters. Since Kerem likes it when things are different, he wants all substrings of his string s to be distinct. Substring is a string formed by some number of consecutive characters of the string. For example, string “aba” has substrings “” (empty substring), “a”, “b”, “a”, “ab”, “ba”, “aba”.
If string s has at least two equal substrings then Kerem will change characters at some positions to some other lowercase English letters. Changing characters is a very tiring job, so Kerem want to perform as few changes as possible.
Your task is to find the minimum number of changes needed to make all the substrings of the given string distinct, or determine that it is impossible.
Input
The first line of the input contains an integer n (1 ≤ n ≤ 100 000) — the length of the string s.
The second line contains the string s of length n consisting of only lowercase English letters.
Output
If it’s impossible to change the string s such that all its substring are distinct print -1. Otherwise print the minimum required number of changes.
Sample Input
Input
2
aa
Output
1
Input
4
koko
Output
2
Input
5
murat
Output
0
给一个长度为n的字符串,问最少需要改变几个字母才能使这个字符串的字串全不相同
全不相同只能是不同的字母组合而成,故n大于26的绝不可能,小于等于26的只需把大于一的改变就行
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int main() { char s[100005]; int n; int m[26]; while(scanf("%d",&n)!=EOF) { memset(m,0,sizeof(m)); scanf("%s",s); int sum=0; int l=strlen(s); if(l>26) { printf("-1\n"); continue;} for(int i=0;i<l;i++) { int t=s[i]-'a'; if(!m[t]) m[t]++; else { sum++; } } printf("%d\n",sum); } return 0; }
相关文章推荐
- Jquery 的bind(), live(), delegate(), on()绑定事件方式
- Node安装文档
- html元素居中方案
- jquery获取当前页面的URL信息
- javascript的理解及经典案例分析
- jQuery源码解析(依赖与架构模块)笔记三------回调函数
- javascript的replace+正则 实现ES6的字符串模版
- 12个JavaScript技巧
- jQuery设置和获取span的内容 (详解)
- javascript中的swith用法
- AngularJs最简单解决跨域问题案例
- [转]TextArea设置MaxLength属性最大输入值的js代码
- AngularJs最简单解决跨域问题案例
- caffe学习笔记(1):数据层定义
- JS中对象与字符串的互相转换详解
- MVC5中后台提供Json,前台处理Json,绑定给Dropdownlist的例子
- 在小公司里前端开发要做什么事?
- HTML5的一些新特性
- js各种间距数据汇总
- jQuery中通过ajax调用webservice传递数组参数的问题实例详解