zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    1052. 爱生气的书店老板
    766. 托普利茨矩阵
    643.子数组的最大平均数I
    450. 删除二叉搜索树中的节点
    1489.找到最小生成树里的关键边和伪关键边
    839相似字符串
    1631.最小体力消耗路径
    SnowFlake雪花算法源码分析&灵活改造,常见分布式ID生成解决方案
    【目标检测】三、Faster R-CNN与R-FCN
    【目标检测】二、Fast R-CNN与SVD
  • 原文地址:https://www.cnblogs.com/Yumesenya/p/5500669.html
Copyright © 2011-2022 走看看