Codeforces Round #352 (Div. 2) B. Different is Good

Posted ( m Lweleth)

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 be different.

Kerem recently got a string s consisting of lowercase English letters. Since Kerem likes it when things are different, he wants all substringsof 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".

 

 超级水,直接数组映射一发就好了。

 

 1 #include <stdio.h>
 2 #include <iostream>
 3 #include <string.h>
 4 #include <algorithm>
 5 #include <map>
 6 #define ll __int64
 7 using namespace std;
 8 
 9 const int INF = 0x3f3f3f3f;
10 map<char,int>b;
11 char a[100010];
12 int main()
13 {
14     int n, ans;
15     char t;
16     cin >> n;
17     getchar();
18     b.clear();
19     ans = 0;
20     for(int i = 0; i < n; i++)
21     {
22         scanf("%c", &t);
23         if(b[t]!=0)
24             ans++;
25         else if(b[t]==0)
26             b[t]++;
27     }
28     if(n > 26)
29         printf("-1\n");
30     else printf("%d\n", ans);
31 
32 }

 

 

 

以上是关于Codeforces Round #352 (Div. 2) B. Different is Good的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #352 (Div. 2) D. Robin Hood

Codeforces Round #436 E. Fire(背包dp+输出路径)

[ACM]Codeforces Round #534 (Div. 2)

Codeforces Round #726 (Div. 2) B. Bad Boy(贪心)

CodeForces 352C-- Jeff and Rounding

Codeforces Global Round 19