http://codeforces.com/problemset/problem/1038/A(题目链接)
You are given a string s of length n, which consists only of the first k letters of the Latin alphabet. All letters in string s are uppercase.
A subsequence of string s is a string that can be derived from s by deleting some of its symbols without changing the order of the remaining symbols. For example, “ADE” and “BD” are subsequences of “ABCDE”, but “DEA” is not.
A subsequence of s called good if the number of occurences of each of the first k letters of the alphabet is the same.
Find the length of the longest good subsequence of s.
Input
The first line of the input contains integers n (1≤n≤1e5) and k (1≤k≤26)
The second line of the input contains the string s of length n. String s only contains uppercase letters from ‘A’ to the k-th letter of Latin alphabet.
Output
Print the only integer — the length of the longest good subsequence of string s.
Examples
input
9 3
ACAABCCAB
output
6
input
9 4
ABCABCABC
output
0
这个题目一开始理解错了,看了解释之后才知道是什么意思。题目的本意是要寻找前k个字母出现次数最长的子字符串。只要找出出现次数最少的那一个,然后乘以k就可以了。上代码,嘤嘤嘤。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define inf 0x3f3f3f3f
using namespace std;
const int maxx=1e5+10;
char a[maxx];
int b[maxx];
int n,k;
int main()
{
while(cin>>n>>k)
{
cin>>a;
int minn=inf;
for(int i=0;i<n;i++) b[a[i]-'A'+1]++;
for(int i=1;i<=k;i++)
minn=min(b[i],minn);
cout<<k*minn<<endl;
}
return 0;
}
每天少做一个题,都要说声对不起。┭┮﹏┭┮