Accept: 2    Submit: 16

Time Limit: 2000 mSec    Memory Limit : 32768 KB

 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

425 5abcdeabcdeabcdeabcdeabcde25 5aaaaabbbbbcccccdddddeeeee25 5adcbadcbedbadedcbacbcadbc3 2aaa

 Sample Output

6150210

 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

第六届福建省大学生程序设计竞赛-重现赛(感谢承办方华侨大学)

题意:给你长度为n的字符串,整个字符串中的字符种类是字母表的前k种,让你找到两个不同的连续子串,这两个子串满足没有重复的元素种类,然后求符合条件的两个字符串的长度的乘积。
思路:用b[state]表示字母状态为state的字母种类的最大长度是多少,然后再求dp[state]表示字母种类状态为state及其子集的最大长度,然后就可以用dp[state]*dp[((1<<k)-1)^state]更新答案了。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 99999999
#define pi acos(-1.0)
#define maxn 2005
char s[maxn];
int b[140000],dp[140000];
int main()
{
int n,m,i,j,T,k,state,state1,num;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&k);
scanf("%s",s+1);
memset(b,0,sizeof(b));
for(i=1;i<=n;i++){
state=0;
for(j=i;j<=n;j++){
state=state|( 1<<(s[j]-'a') ) ;
b[state]=max(b[state],j-i+1);
}
}
dp[0]=0;
for(state=1;state<=(1<<k)-1;state++){
dp[state]=b[state];
for(j=1;j<=k;j++){
if(state&(1<<(j-1)) ){
state1=state-(1<<(j-1));
dp[state]=max(dp[state],dp[state1]);
}
}
}
num=0;
for(state=1;state<=(1<<k)-1;state++){
state1=((1<<k)-1)^state;
num=max(num,dp[state]*dp[state1]); }
printf("%d\n",num);
}
return 0;
}

fzu2218 Simple String Problem的更多相关文章

  1. FZU2218 Simple String Problem(状压DP)

    首先,定义S,表示前k个字符出现的集合,用二进制来压缩. 接下来,推出dp1[S],表示集合为S的子串的最长长度. 然后根据dp1[S]再推出dp2[S],表示集合为S或S的子集的子串的最长长度. 最 ...

  2. FZU-2218 Simple String Problem(状态压缩DP)

      原题地址: 题意: 给你一个串和两个整数n和k,n表示串的长度,k表示串只有前k个小写字母,问你两个不含相同元素的连续子串的长度的最大乘积. 思路: 状态压缩DP最多16位,第i位的状态表示第i位 ...

  3. (比赛)A - Simple String Problem

    A - Simple String Problem Time Limit:10000MS     Memory Limit:65536KB     64bit IO Format:%lld & ...

  4. FZU - 2218 Simple String Problem(状压dp)

    Simple String Problem Recently, you have found your interest in string theory. Here is an interestin ...

  5. FZU - 2218 Simple String Problem 状压dp

    FZU - 2218Simple String Problem 题目大意:给一个长度为n含有k个不同字母的串,从中挑选出两个连续的子串,要求两个子串中含有不同的字符,问这样的两个子串长度乘积最大是多少 ...

  6. FZU 2218 Simple String Problem(简单字符串问题)

    Description 题目描述 Recently, you have found your interest in string theory. Here is an interesting que ...

  7. Water --- CSU 1550: Simple String

    Simple String Problem's Link:   http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1550 Mean: 略. analy ...

  8. hdu 1757 A Simple Math Problem (乘法矩阵)

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  9. hdu 5772 String problem 最大权闭合子图

    String problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5772 Description This is a simple pro ...

随机推荐

  1. oracle坚决不挂2(SQLPLUS基础命令)

    继续复习!!SQLplus基础命令,其实这个应该是第一个要复习的.因为基础,你懂得..要想学会跑,你先得知道该怎么走吧. win+R 输入cmd ,我们开始启动SQLplus sqlplus user ...

  2. python基础语法1-变量

    l Python基础语法1-变量 

  3. 【ORA】ORA-01756: quoted string not properly terminated

    出现ORA-01756: quoted string not properly terminated 后,查看SQL是否有中文符号 修改为英文的符号,运行正常

  4. 为什么不建议用var

    看了这个例子估计你就会明白了 var a = 'global'; function test() { if (!a) { var a = 'part'; } console.log(a); } tes ...

  5. QT串口助手(二):参数配置

    作者:zzssdd2 E-mail:zzssdd2@foxmail.com 一.前言 主要实现功能 串口参数的配置:波特率.数据位.停止位.校验位 本机串口设备的查询与添加显示 串口设备的手动更新与打 ...

  6. 1V升3V芯片,1V升3.3V芯片,大电流的,低功耗

    一般来说,1V的电压实在很低了,即使是干电池的话,再1V时,也是基本属于没电状态了.还有一种是干电池输出电流大时,也会把干电池的电压从1.5V拉低到1V左右. 更多的是客户对于1V时要能升到3V或者3 ...

  7. Centos7 添加用户及设置权限

    一.添加用户 1.登录root 用户 [gau@localhost /]$ su Password: # 输入密码 [root@localhost /]# 2.添加用户 [root@localhost ...

  8. Flink的状态与容错

    本文主要运行到Flink以下内容 检查点机制(CheckPoint) 状态管理器(StateBackend) 状态周期(StateTtlConfig) 关系 首先要将state和checkpoint概 ...

  9. C++11中string与数值类型的转换

    C++中string与数值类型的相互转换记录 string转int.double.long string s = "123.456"; // string -> int co ...

  10. Connection Manager简称connman

    ConnMan    Connection Manager简称connman,connman是使用d-bus做为进程间通信机制来管理Linux网络链接的一种软件.在connman的d-bus接口中,有 ...