CodeForces 672B Different is Good

链接:传送门
题意:给出一个串s,改变s串中的一些字符使得其所有子串两两不相同,求出最小改变次数,如果无论怎么改变一些字符都无法避免子串两两相同则输出-1。
思路:如果长度>26肯定是无解,如果长度在26之内,记录每个字符出现的次数,如果超过一次计数器就+1

/*************************************************************************
    > File Name: A.cpp
    > Author:    WArobot 
    > Blog:      http://www.cnblogs.com/WArobot/ 
    > Created Time: 2017年04月17日 星期一 20时03分04秒
 ************************************************************************/

#include<bits/stdc++.h>
using namespace std;

char s[1000000];
int flag[26];
int n;
int main(){
	while(scanf("%d",&n)!=EOF){
		scanf("%s",s);
		if(n>26)	printf("-1
");
		else{
			memset(flag,0,sizeof(flag));
			int cnt = 0;
			for(int i=0;i<n;i++)
				flag[ s[i]-'a' ]++;
			for(int i=0;i<26;i++){
				if(flag[i]==0) continue;
				else	cnt += flag[i]-1;
			}
			printf("%d
",cnt);
		}
	}
	return 0;
}
原文地址:https://www.cnblogs.com/WArobot/p/6725691.html