# fzu 2218 Simple String Problem 状压dp

Problem 2218 Simple String Problem

## Problem Description

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 5adcbadcbedbadedcbacbcadbc
3 2
aaa

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.

## Source

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define N 2005

using namespace std;

int dp[(1<<16)+15];
int dp1[(1<<16)+15];
char str[N];
int n,k;

int main()
{
int T;
cin>>T;
while(T--)
{
cin>>n>>k;
memset(dp,0,sizeof(dp));
memset(dp1,0,sizeof(dp1));
scanf("%s",str+1);
for(int i=1;i<=n;i++)  //   求出s  状态的 最大 len
{
int s=0;
for(int j=i;j<=n;j++)
{
int t= str[j] - 'a';
s=s|(1<<t);
dp[s]=max(dp[s],j-i+1);
}
}

int up=1<<k;
for(int i=0;i<up;i++) dp1[i]=dp[i];

for(int s=0;s<up;s++)  // 求出s 状态的子集中的 最大 len
{
for(int j=0;j<k;j++)
{
if(s&(1<<j)){
int tmps=s-(1<<j);
dp1[s]=max(dp1[s],dp1[tmps]);
}
}
}

int ans=0;
for(int s=0;s<up;s++)
{
int tmps=((1<<k)-1)^s;
ans=max(ans,dp1[s]*dp1[tmps]);
}

cout<<ans<<endl;
}

return 0;
}