Codeforces Round #352 (Div. 2) B
2 seconds
256 megabytes
standard input
standard output
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.
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.
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.
2
aa
1
4
koko
2
5
murat
0
In the first sample one of the possible solutions is to change the first character to 'b'.
In the second sample, one may change the first character to 'a' and second character to 'b', so the string becomes "abko".
题意:长度为n的小写字母字符, 要求分解之后 不能有相同的字符串 ,问需要更改多少个字符,当无法实现时输出-1
题解:分析可知 当n>26 必然存在相同字符 输出-1
n<=26 处理 计算重复的数量 输出
#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<queue>
#include<stack>
#define ll __int64
#define pi acos(-1.0)
using namespace std;
char a[];
map<char,int>mp;
int n;
int main()
{
scanf("%d",&n);
mp.clear();
getchar();
for(int i=;i<=n;i++)
{
scanf("%c",&a[i]);
mp[a[i]]++;
}
if(n>)
{
cout<<"-1"<<endl;
return ;
}
int ans=;
for(int i='a';i<='z';i++)
{
if(mp[i]>)
{
ans=ans+mp[i]-;
}
}
cout<<ans<<endl;
return ;
}
Codeforces Round #352 (Div. 2) B的更多相关文章
- Codeforces Round #352 (Div. 2) C. Recycling Bottles 暴力+贪心
题目链接: http://codeforces.com/contest/672/problem/C 题意: 公园里有两个人一个垃圾桶和n个瓶子,现在这两个人需要把所有的瓶子扔进垃圾桶,给出人,垃圾桶, ...
- Codeforces Round #352 (Div. 2) D. Robin Hood
题目链接: http://codeforces.com/contest/672/problem/D 题意: 给你一个数组,每次操作,最大数减一,最小数加一,如果最大数减一之后比最小数加一之后要小,则取 ...
- Codeforces Round #352 (Div. 2) D. Robin Hood (二分答案)
题目链接:http://codeforces.com/contest/672/problem/D 有n个人,k个操作,每个人有a[i]个物品,每次操作把最富的人那里拿一个物品给最穷的人,问你最后贫富差 ...
- Codeforces Round #352 (Div. 1) B. Robin Hood 二分
B. Robin Hood 题目连接: http://www.codeforces.com/contest/671/problem/B Description We all know the impr ...
- Codeforces Round #352 (Div. 1) A. Recycling Bottles 暴力
A. Recycling Bottles 题目连接: http://www.codeforces.com/contest/671/problem/A Description It was recycl ...
- Codeforces Round #352 (Div. 2) B. Different is Good 水题
B. Different is Good 题目连接: http://www.codeforces.com/contest/672/problem/B Description A wise man to ...
- Codeforces Round #352 (Div. 2) A. Summer Camp 水题
A. Summer Camp 题目连接: http://www.codeforces.com/contest/672/problem/A Description Every year, hundred ...
- Codeforces Round #352 (Div. 2) ABCD
Problems # Name A Summer Camp standard input/output 1 s, 256 MB x3197 B Different is Good ...
- Codeforces Round #352 (Div. 2)
模拟 A - Summer Camp #include <bits/stdc++.h> int a[1100]; int b[100]; int len; void init() { in ...
- Codeforces Round #352 (Div. 2) B - Different is Good
A wise man told Kerem "Different is good" once, so Kerem wants all things in his life to b ...
随机推荐
- Java的按值传递和按引用传递解说
在网上看到的一个帖子解释Java的按值传递和按引用传递,感觉挺全面,就转过来,以供以后学习参考: 1:按值传递是什么 指的是在方法调用时,传递的参数是按值的拷贝传递.示例如下: public clas ...
- SpringMVC系列一
首先介绍一下SpringMVC的执行流程:如图 1.用户在客户端发送请求,经过前端控制器DispatcherServlet,请求处理器处理,返回ModelAndView返回结果到前端控制器: 2.前端 ...
- python核心编程2 第八章 练习
8–2. 循环. 编写一个程序, 让用户输入三个数字: (f)rom, (t)o, 和 (i)ncrement . 以 i为步长, 从 f 计数到 t , 包括 f 和 t . 例如, 如果输入的是 ...
- html中table,tr,td
table表格,tr表格中的行,tr表格中的列,等级关系是table>tr>td, 当然表格中还包括thead,tbody,tfoot,th,但由于浏览器支持缘故很少使用.另外table在 ...
- mongdb安装配置
一.先登录Mongodb官网https://www.mongodb.com/download-center#community 下载 安装包.32.64位的都行. 或者查看我的百度云(使用win7 ...
- mysql 按值排序
order by FIELD(AuditorStatus,3,0,1,2) laravel 写法 $data = $query->orderByRaw(\DB::Raw('FIELD(Audit ...
- 在ubuntu上安装subline
Sublime Text is a most popular, lightweight and smart cross-platform text and source code editor wit ...
- Android内购订单验证 --- php实现
直接上代码: function googleVerify($sdata,$google_public_key) { $sdata = json_decode($sdata,true); $in_app ...
- flask项目实战--论坛
项目结构搭建 1:用pycharm创建flask bbs项目 2:分别创建config.py.exts.py.models.py.manage.py文件 创建一个apps包存放前台,后台,公共的模块 ...
- SGU495 概率DP
Kids and Prizes ICPC (International Cardboard Producing Company) is in the business of producing car ...