题目链接:

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. Redis命令行之List

    一.Redis之List简介 1. List是简单的字符串列表,按照插入顺序排列. 2. 一个列表最多可存储232-1个元素(40多亿). 二.Redis之List命令行操作 Lrange:获取列表指 ...

  2. .NET Core 3.0之创建基于Consul的Configuration扩展组件

    写在前面 经过前面三篇关于.NET Core Configuration的文章之后,本篇文章主要讨论如何扩展一个Configuration组件出来.如果前面三篇文章没有看到,可以点击如下地址访问 .N ...

  3. Oracle 实现 一个关键字 匹配多个 字段

    有这么一个需求,满足只有一个输入框的条件下,支持不同数据列的搜索结果. 说白了,就是这个 输入框  既可以用来 搜索姓名,也可以搜索 年龄,地址等. 分析: 一般情况下,我们的一个输入框对应 数据库 ...

  4. T2832 6个朋友 codevs

    http://codevs.cn/problem/2832/  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题目描述 Description 有这么一种说法: ...

  5. 《Java虚拟机原理图解》 1.2、class文件中的常量池

    了解JVM虚拟机原理 是每一个Java程序员修炼的必经之路.但是由于JVM虚拟机中有很多的东西讲述的比较宽泛,在当前接触到的关于JVM虚拟机原理的教程或者博客中,绝大部分都是充斥的文字性的描述,很难给 ...

  6. 小程序 - Template

    关于模板,参见:https://mp.weixin.qq.com/debug/wxadoc/dev/framework/view/wxml/template.html 引入模块 <import ...

  7. A&DCTF

    ADCTF  WRITEUP 方向:Reverse  解题数:2 题目:Reverse_01 解题过程: 用ida打开反汇编查看代码,看main函数发现 关键部分,字符串比较,竟然是直接比较”is_t ...

  8. jk_proxy实现apache+tomcat负载均衡

    Apache + tomcat实现server集群 主要參照:http://blog.csdn.net/welun521/article/details/4169879 watermark/2/tex ...

  9. Redis 事务及其应用

    参考: http://www.runoob.com/redis/redis-transactions.html https://www.cnblogs.com/qlshine/p/5958504.ht ...

  10. mysql limit分页优化方法分享

    同样是取10条数据  select * from yanxue8_visit limit 10000,10 和  select * from yanxue8_visit limit 0,10  就不是 ...