您的位置:首页 > Web前端

CodeForces-672B-Different is Good

2016-05-20 09:44 176 查看
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

Input

2

aa

Output

1

Input

4

koko

Output

2

Input

5

murat

Output

0

给一个长度为n的字符串,问最少需要改变几个字母才能使这个字符串的字串全不相同

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

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
char s[100005];
int n;
int m[26];
while(scanf("%d",&n)!=EOF)
{
memset(m,0,sizeof(m));
scanf("%s",s);
int sum=0;
int l=strlen(s);
if(l>26) { printf("-1\n"); continue;}
for(int i=0;i<l;i++)
{
int t=s[i]-'a';
if(!m[t])
m[t]++;
else
{
sum++;
}
}
printf("%d\n",sum);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: