zoukankan      html  css  js  c++  java
  • codeforces 672B B. Different is Good(水题)

    题目链接:

    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 allsubstrings 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的字符串,问最少需要改变几个字母才能使这个字符串的字串全不相同;

    思路:

    全不相同只能是不同的字母组合而成,故n大于26的绝不可能,小于等于26的只需把大于一的改变就行;

    AC代码:

    #include <bits/stdc++.h>
    /*#include <iostream>
    #include <queue>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include <cstdio>
    */
    using namespace std;
    #define Riep(n) for(int i=1;i<=n;i++)
    #define Riop(n) for(int i=0;i<n;i++)
    #define Rjep(n) for(int j=1;j<=n;j++)
    #define Rjop(n) for(int j=0;j<n;j++)
    #define mst(ss,b) memset(ss,b,sizeof(ss));
    typedef long long LL;
    const LL mod=1e9+7;
    const double PI=acos(-1.0);
    const int inf=0x3f3f3f3f;
    const int N=2e5+5;
    
    int n,flag[30];
    char str[N];
    int main()
    {
    
        scanf("%d",&n);
        scanf("%s",str);
        mst(flag,0);
        if(n>=27)cout<<"-1"<<endl;
        else
        {
        for(int i=0;i<n;i++)
        {
            flag[str[i]-'a']++;
        }
        int ans=0;
        for(int i=0;i<26;i++)
        {
            if(flag[i]>1)
            {
                ans+=flag[i]-1;
            }
        }
        cout<<ans<<endl;
        }
    
        return 0;
    }
  • 相关阅读:
    编写 grunt 插件经验
    Sencha Touch 手机移动开发框架 HTML5 项目压缩方案;
    随笔 编辑推荐 上头条了, 贴出来做个记念!
    Javascript 俄罗斯方块 游戏代码解释!
    30天自制操作系统(NASM+GCC版)
    Logisim 打不开的解决方案(Windows10)
    Kali Linux 2020通过UEFI硬盘安装(免u盘)
    开源一个自制的ORM框架,基于Java原生JDBC(应该是全网首个吧)
    书单
    前端技术文章收集
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5484521.html
Copyright © 2011-2022 走看看