题目链接:

B. Different is Good

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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 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.

 
Examples
 
input
2
aa
output
1
input
4
koko
output
2
input
5
murat
output
0
Note

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的字符串,问最少需要改变几个字母才能使这个字符串的字串全不相同;

思路:

全不相同只能是不同的字母组合而成,故n大于26的绝不可能,小于等于26的只需把大于一的改变就行;

AC代码:

#include <bits/stdc++.h>
/*#include <iostream>
#include <queue>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdio>
*/
using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
const LL mod=1e9+;
const double PI=acos(-1.0);
const int inf=0x3f3f3f3f;
const int N=2e5+; int n,flag[];
char str[N];
int main()
{ scanf("%d",&n);
scanf("%s",str);
mst(flag,);
if(n>=)cout<<"-1"<<endl;
else
{
for(int i=;i<n;i++)
{
flag[str[i]-'a']++;
}
int ans=;
for(int i=;i<;i++)
{
if(flag[i]>)
{
ans+=flag[i]-;
}
}
cout<<ans<<endl;
} return ;
}

codeforces 672B B. Different is Good(水题)的更多相关文章

  1. Educational Codeforces Round 7 B. The Time 水题

    B. The Time 题目连接: http://www.codeforces.com/contest/622/problem/B Description You are given the curr ...

  2. Educational Codeforces Round 7 A. Infinite Sequence 水题

    A. Infinite Sequence 题目连接: http://www.codeforces.com/contest/622/problem/A Description Consider the ...

  3. Codeforces Testing Round #12 A. Divisibility 水题

    A. Divisibility Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/597/probl ...

  4. Codeforces Beta Round #37 A. Towers 水题

    A. Towers 题目连接: http://www.codeforces.com/contest/37/problem/A Description Little Vasya has received ...

  5. codeforces 677A A. Vanya and Fence(水题)

    题目链接: A. Vanya and Fence time limit per test 1 second memory limit per test 256 megabytes input stan ...

  6. CodeForces 690C1 Brain Network (easy) (水题,判断树)

    题意:给定 n 条边,判断是不是树. 析:水题,判断是不是树,首先是有没有环,这个可以用并查集来判断,然后就是边数等于顶点数减1. 代码如下: #include <bits/stdc++.h&g ...

  7. Codeforces - 1194B - Yet Another Crosses Problem - 水题

    https://codeforc.es/contest/1194/problem/B 好像也没什么思维,就是一个水题,不过蛮有趣的.意思是找缺黑色最少的行列十字.用O(n)的空间预处理掉一维,然后用O ...

  8. Codeforces 1082B Vova and Trophies 模拟,水题,坑 B

    Codeforces 1082B Vova and Trophies https://vjudge.net/problem/CodeForces-1082B 题目: Vova has won nn t ...

  9. CodeForces 686A Free Ice Cream (水题模拟)

    题意:给定初始数量的冰激凌,然后n个操作,如果是“+”,那么数量就会增加,如果是“-”,如果现有的数量大于等于要减的数量,那么就减掉,如果小于, 那么孩子就会离家.问你最后剩下多少冰激凌,和出走的孩子 ...

随机推荐

  1. Nginx+Uwsgi+Django以及解决的一些问题

    1.pip3 install uwsgi,项目目录路径:/data/my_env1/monitor1/,项目名:monitor1,app名:show 测试启动: ln -s /data/linkdoo ...

  2. File类 递归 获取目录下所有文件文件夹

    package com.xiwi; import java.io.*; import java.util.*; class file{ public static void main(String a ...

  3. Java普通员工管理系统

    login GUI界面(登录) package 普通员工管理系统; import java.awt.event.ActionEvent; import java.awt.event.ActionLis ...

  4. SolidEdge如何绘制阵列之后取消掉某一些

    在最后一步点击"抑制事件",然后可以在被阵列的圆形上单击,被抑制的圆形变成灰色                    

  5. linux字符设备驱动程序框架(老方法)

    #include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> #inclu ...

  6. (六)Unity5.0新特性------新动画功能

     unity 5.0 中的新动画功能 这里是你能够期待的新动画功能高速概述 ! State Machine Behaviours状态机行为 在Unity 5 中,你会能够将StateMachine ...

  7. 还在为开发APP发愁? 这里就有现成通用的代码!

    1.开源控件 1)首页: 1.1)首先是下拉刷新数据的 SwipeRefreshLayout 地址:https://github.com/hanks-zyh/SwipeRefreshLayout 1. ...

  8. Python开发【迭代器】

    1.迭代器 1.1.迭代器创建:指定数据创建迭代器(使用iter()和next() ) x = [1, 2, 3] #定义一个列表:<class 'list'> y = iter(x) # ...

  9. effctive C++ 读书笔记 条款 16

    条款16 成对使用new和delete时要採取同样形式 #include <iostream> #include <string> using namespace std; / ...

  10. 将异常(getStackTrace)转化成String

    方法一: private static String getStackMsg(Exception e) { StringBuffer sb = new StringBuffer(); StackTra ...