CodeForces 672B Different is Good
2016-05-14 20:39
741 查看
思路:子串不同,其实就是要每个字符都不同,记录一下就OK了
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 allsubstrings 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
Output
Input
Output
Input
Output
#include<bits\stdc++.h> using namespace std; string s; int vis[26]; int main() { int n; scanf("%d",&n); cin >> s; for (int i = 0;i<s.size();i++) vis[s[i]-'a']++; int a = 0,b=0; for (int i = 0;i<26;i++) { if(vis[i]==0) b++; else a+=(vis[i]-1); } if (b<a) puts("-1"); else printf("%d\n",a); }
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 allsubstrings 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
相关文章推荐
- 剑指Offer--042-左旋转字符串
- 匿名函数和字面量——JS学习
- JS 将字符串转换成日期类型
- JS学习笔记
- HTML 5实现图像走马灯效果
- BZOJ 1013 [JSOI2008]球形空间产生器sphere
- 关于json对象和数组的访问的一些问题
- CSS Hack解决两个阵营:①IE8、IE9②IE6、IE7、FireFox
- CSS Hack 学习笔记
- JavaScript绑定事件监听函数的通用方法
- js判断checkbox是否选中
- 前端之项目管理
- 使用CSS Hack解决IE6双倍边距Bug
- jquery tmpl 详解
- 前端模块化开发
- JavaScript对象
- 前端规范(ES6\BEM\OOCSS\SMACSS)
- jQuery打飞机游戏代码下载
- CSS 不规整元素的宽高等比例
- CSS 多列等高