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 ...
随机推荐
- js动画之无缝滚动
效果图如下: HTML代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charse ...
- 《JSON笔记之三》---postman中传入json串
1.关于如何使用postman工具,简单的介绍一下, 用户在开发或者调试网络程序或者是网页B/S模式的程序的时候是需要一些方法来跟踪网页请求的,用户可以使用一些网络的监视工具比如著名的Firebug等 ...
- Docker学习之镜像操作
使用Docker镜像 以下都是Docker镜像的一系列重要名操作,包括获取.查看.搜索.删除.创建.存出或载入.上传等.可使用docker image help命令查看帮助. 1.获取镜像(pull) ...
- 彻底搞定C指针--“函数名与函数指针”
函数名与函数指针 一 通常的函数调用 一个通常的函数调用的例子: //自行包含头文件 void MyFun(int x); //此处的申明也可写成:void MyFun( int ); 点击打开链 ...
- TCL之表达式
- php中const与static的区别与使用(转)
首先关于const 在php的类内部只可以修饰成员属性,不可以修饰方法,如下: class Test{ const PATH = 'c/';//修饰常量 const function te ...
- 01 mysql 基础一 (进阶)
mysql基础一 1.认识mysql与创建用户 01 Mysql简介 Mysql是最流行的关系型数据库管理系统之一,由瑞典MySQLAB公司开发,目前属于Oracle公司. MySQL是一种关联数据库 ...
- GDOI--DAY2 游记
今天,熬夜不够多,果然,不出所料,爆零了... 第一题,看到数据之大,懵逼了,于是,敲了个二分SPFA,但是!最大的点GG了,呜呜~~~~(>_<)~~~~ ,于是,就不继续做第一题了(虽 ...
- VIM安装YCM插件
折腾了两天,终于好了 1.配置VIM (1)下载相关插件 sudo apt-get install git sudo apt-get install build-essential cmake sud ...
- B1005 继续(3n+1)猜想 (25分)
B1005 继续(3n+1)猜想 (25分) 卡拉兹(Callatz)猜想已经在1001中给出了描述.在这个题目里,情况稍微有些复杂. 当我们验证卡拉兹猜想的时候,为了避免重复计算,可以记录下递推过程 ...