zoukankan      html  css  js  c++  java
  • Codeforces Round #352 (Div. 2) B

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

    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的小写字母字符, 要求分解之后 不能有相同的字符串 ,问需要更改多少个字符,当无法实现时输出-1

    题解:分析可知 当n>26 必然存在相同字符 输出-1

                         n<=26 处理 计算重复的数量  输出

     1 #include<bits/stdc++.h>
     2 #include<iostream>
     3 #include<cstring>
     4 #include<cstdio>
     5 #include<map>
     6 #include<queue>
     7 #include<stack>
     8 #define ll __int64
     9 #define pi acos(-1.0)
    10 using namespace std;
    11 char a[100005];
    12 map<char,int>mp;
    13 int n;
    14 int main()
    15 {
    16    scanf("%d",&n);
    17    mp.clear();
    18    getchar();
    19    for(int i=1;i<=n;i++)
    20    {
    21     scanf("%c",&a[i]);
    22     mp[a[i]]++;
    23    }
    24    if(n>26)
    25    {
    26        cout<<"-1"<<endl;
    27        return 0;
    28    }
    29    int ans=0;
    30    for(int i='a';i<='z';i++)
    31    {
    32        if(mp[i]>1)
    33        {
    34            ans=ans+mp[i]-1;
    35        }
    36    }
    37    cout<<ans<<endl;
    38     return 0;
    39 } 
  • 相关阅读:
    Rhino 是一个完全使用Java语言编写的开源JavaScript实现。Rhino通常用于在Java程序中,为最终用户提供脚本化能力。它被作为J2SE 6上的默认Java脚本化引擎。
    VS的快捷键F12改成和ECLIPSE一样用ctrl+点击下载线
    到底要不要拆分函数
    “DllRegisterServer的调用失败”问题解决办法(转)
    select into的缺点
    win8 下脚本安装IIS
    快速打开IIS的方法
    windows下硬盘的逻辑结构
    sql server 2005/2008R2 报“红叉”错,即“不允许所请求的注册表访问权”的错误
    rundll32.exe的相关使用语句
  • 原文地址:https://www.cnblogs.com/hsd-/p/5487173.html
Copyright © 2011-2022 走看看