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 }
  • 相关阅读:
    1.表单标签
    07.Ajax.post
    06.Ajax.get
    05.Ajax.get
    04.Ajax-get.html
    03.post.file
    nodejs-7.2. CURD数据管理系统小栗子
    nodejs-7.1. mongoose模块
    JS 无缝轮播图1-节点操作
    JS 放大镜特效
  • 原文地址:https://www.cnblogs.com/Yumesenya/p/5500669.html
Copyright © 2011-2022 走看看