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 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
2
aa
Sample Output
1
题意
给你一个字符串,你可以把这个字符串的字符改成其他的字符,然后让你使得里面的所有子串都不一样
问你最少改多少个
题解:
子串都不一样的话,其实就是所有的字符都不一样,记录一下这个就好了
代码
#include<bits/stdc++.h>
using namespace std;
string s;
int m[26];
int main()
{
cin>>s;
cin>>s;
for(int i=0;i<s.size();i++)
m[s[i]-'a']++;
int add1=0,add2=0;
for(int i=0;i<26;i++)
{
if(m[i]==0)add2++;
else add1+=(m[i]-1);
}
if(add2<add1)cout<<"-1"<<endl;
else cout<<add1<<endl;
}
Codeforces Round #352 (Div. 2) B. Different is Good 水题的更多相关文章
- Codeforces Round #297 (Div. 2)A. Vitaliy and Pie 水题
Codeforces Round #297 (Div. 2)A. Vitaliy and Pie Time Limit: 2 Sec Memory Limit: 256 MBSubmit: xxx ...
- Codeforces Round #290 (Div. 2) A. Fox And Snake 水题
A. Fox And Snake 题目连接: http://codeforces.com/contest/510/problem/A Description Fox Ciel starts to le ...
- Codeforces Round #322 (Div. 2) A. Vasya the Hipster 水题
A. Vasya the Hipster Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/581/p ...
- Codeforces Round #373 (Div. 2) B. Anatoly and Cockroaches 水题
B. Anatoly and Cockroaches 题目连接: http://codeforces.com/contest/719/problem/B Description Anatoly liv ...
- Codeforces Round #368 (Div. 2) A. Brain's Photos 水题
A. Brain's Photos 题目连接: http://www.codeforces.com/contest/707/problem/A Description Small, but very ...
- Codeforces Round #359 (Div. 2) A. Free Ice Cream 水题
A. Free Ice Cream 题目连接: http://www.codeforces.com/contest/686/problem/A Description After their adve ...
- Codeforces Round #355 (Div. 2) A. Vanya and Fence 水题
A. Vanya and Fence 题目连接: http://www.codeforces.com/contest/677/problem/A Description Vanya and his f ...
- Codeforces Round #384 (Div. 2) A. Vladik and flights 水题
A. Vladik and flights 题目链接 http://codeforces.com/contest/743/problem/A 题面 Vladik is a competitive pr ...
- Codeforces Round #379 (Div. 2) D. Anton and Chess 水题
D. Anton and Chess 题目连接: http://codeforces.com/contest/734/problem/D Description Anton likes to play ...
随机推荐
- 【hdu6334】【2018Multi-University-Training Contest04】Problem C. Problems on a Tree
维护1边的联通块和2边的联通块,合并的时候直接启发式合并. cdqz的大爷好强啊. #include<bits/stdc++.h> #define lson (o<<1) #d ...
- RestTemplate中文乱码问题(spring-web3.0.7版本)
从网上找的方法: 方法一: //resttemplate乱码问题 //3.1.X以上版本使用 // restTemplate.getMessageConverters().add(0, StringH ...
- Java WebService Axis 初探
最近在学习WebService 开始了: 一:服务端的编写与发布 1. 工具准备: java的开发环境(这里就不多说了). axis2官网上下载最新的就可以了(我这里用的是axis2-1.4.1- ...
- handlermethodargumentresolver
http://www.cnblogs.com/fangjian0423/p/springMVC-request-param-analysis.html http://www.cnblogs.com/f ...
- 基于vue配置axios
转载地址:https://juejin.im/post/5a02a898f265da43052e0c85 1.背景 在项目开发中ajax请求是必不可缺少 一部分ajax请求不需要loading或则请求 ...
- elasticsearch5.5-head
修改 elasticsearch/config/elasticsearch.yml 添加 http.cors.enabled: true http.cors.allow-origin: "* ...
- angular可自定义的对话框,弹窗指令
指令不明的,推荐 AngularJS指令参数详解 github地址 以下为示例代码 <!DOCTYPE html> <html lang="en" ng-app= ...
- linux下快速安装emacs方法
背景 在公司工作的时候经常需要在很多服务器之间切换,而公司的服务器上一般都没emacs,因此总结一下快速安装emacs的方法. 最简单的是直接使用yum安装,但是有两个问题,一个是有的生产服务器直接没 ...
- Linux学习笔记:rm删除文件和文件夹
使用rm命令删除一个文件或者目录 使用rmdir可以删除空文件夹 参数: -i:删除前逐一询问确认 -f:即使原档案属性设为唯读,亦直接删除,无需逐一确认 -r:递归 删除文件可以直接使用rm命令,若 ...
- 1926: [Sdoi2010]粟粟的书架
大概就是分情况乱搞.. 经典维护二维前缀和暴力+莫队算法 垫底QAQ #include <bits/stdc++.h> using namespace std; namespace my_ ...