Recently, you have found your interest in string theory. Here is an interesting question about strings.
You are given a string S of length n consisting of the first k lowercase letters.
You are required to find two non-empty substrings (note that substrings must be consecutive) of S, such that the two substrings don't share any same letter. Here comes the question, what is the maximum product of the two substring lengths?
Input
The first line contains an integer T, meaning the number of the cases. 1 <= T <= 50.
For each test case, the first line consists of two integers n and k. (1 <= n <= 2000, 1 <= k <= 16).
The second line is a string of length n, consisting only the first k lowercase letters in the alphabet. For example, when k = 3, it consists of a, b, and c.
Output
For each test case, output the answer of the question.
Sample Input
4 25 5 abcdeabcdeabcdeabcdeabcde 25 5 aaaaabbbbbcccccdddddeeeee 25 5 adcbadcbedbadedcbacbcadbc 3 2 aaa
Sample Output
6 150 21 0
Hint
One possible option for the two chosen substrings for the first sample is "abc" and "de".
The two chosen substrings for the third sample are "ded" and "cbacbca".
In the fourth sample, we can't choose such two non-empty substrings, so the answer is 0.
题意:给你一个长度为n的由前k个小写字母组成的字符串,然后让你求两个字串的长度的乘积的最大值
这两个字串的限制要求是(每个字串必须连续,两个字串不能相交,两个字串的字母组成集合不能相交,
就是说第一个字符串是abcdef 那么第二个字符串就不能包含 abcdef 这6个字母中的任何一个)
思路:观察数据规模,字符串最多由16种字母组成,所以可以把所有状态的字符串长度求出来
然后枚举每个状态和它的反状态的乘积,取最大值就行了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
char a[maxn];
int dp[maxn];
int main() {
int T,n,k,state;
scanf("%d",&T);
while(T--) {
scanf("%d%d",&n,&k);
getchar();
scanf("%s",a);
memset(dp,0,sizeof(dp));
for(int i=0;i<n;i++) {
state=0;
for(int j=i;j<n;j++) {
state|=(1<<(a[j]-'a'));
dp[state]=max(dp[state],(j-i+1));
}
}
int s=1<<k;
for(int i=0;i<s;i++) {
for(int j=0;j<k;j++) {
if(i&(1<<j))
dp[i]=max(dp[i],dp[i^(1<<j)]);
}
}
int ans=0;
for(int i=0;i<s;i++)
{
ans=max(ans,dp[i]*dp[i^(s-1)]);
}
printf("%d\n",ans);
}
return 0;//
}