• 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
    ");
    30     else printf("%d
    ", ans);
    31 
    32 }
  • 相关阅读:
    Linux日志管理系统rsyslog
    Linux访问权限控制及时间同步实践
    Linux系统自动化安装之cobbler实现
    【转】java取整和java四舍五入方法
    The web application [ ] registered the JDBC driver [net.sourceforge.jtds.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver
    Python学习笔记之函数式编程
    Java去重字符串的两种方法以及java中冒号的使用
    Python学习之字符串格式化
    Python学习之文件操作
    Python学习笔记之爬虫
  • 原文地址:https://www.cnblogs.com/Yumesenya/p/5500669.html
Copyright © 2020-2023  润新知