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 }
  • 相关阅读:
    好书推介《实战机器学*》
    Web技术图书名单
    大数据技术书,看看有没有感兴趣的
    博客园设置自定义皮肤,添加自定义小模块悬浮天气组件,github图标链接等
    Final Cut Pro 视频剪辑学习记录,快捷操作等
    css 利用 clip-path 裁剪多边形,三角形,梯形,六边形等
    有呀,有呀,设计!有呀,有呀,组件!
    github README添加badge标识,多彩的tag标签
    vue timeline 开箱即用的时间轴组件,日志更新时间轴组件
    那些需要收藏的网站网址
  • 原文地址:https://www.cnblogs.com/Yumesenya/p/5500669.html
Copyright © 2011-2022 走看看