Simple String Problem

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,n表示串的长度,k表示串只有前k个小写字母,问你两个不含相同元素的连续子串的长度的最大乘积。

第一道状压dp。详见代码。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<string>
#include<math.h>
#include<algorithm>
#define MAX 2005
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll; int dp[(<<)+]; //dp状态存储16个字母的存在情况,1存在0不存在
int max(int x,int y){
return x>y?x:y;
}
int main()
{
int t,n,k,i,j;
char s[MAX];
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&k);
scanf(" %s",s);
memset(dp,,sizeof(dp));
for(i=;i<n;i++){ //O(n^2)i首j尾遍历字符串全部子串,在遍历的同时每加入新字符需要及时更新状态
int tt=;
for(j=i;j<n;j++){
tt|=<<(s[j]-'a'); //|=相当于加入s[j]字符
dp[tt]=max(dp[tt],j-i+); //获取含某几种字符的子串最长长度
}
}
//上一步仅仅是获取了含某几种字符的最长长度,还需要考虑子问题:比如某状态有三种字符长度为4,而另一种状态仅有其中的两种字符长度就达到6,我们要使两个不含相同元素子串长度乘积最大,那么在不会有重叠字符的前提下,长度当然越大越好,所以还要更新子问题
for(i=;i<(<<k);i++){ //枚举所有状态
for(j=;j<k;j++){ //枚举每种字符
if(i&(<<j)){ //若i状态中有第j种字符
dp[i]=max(dp[i],dp[i^(<<j)]); //i状态去掉j字符的子问题(异或异为真,i^00000保持原状,仅^含1位变0)
}
}
}
int maxx=;
for(i=;i<(<<k);i++){ //寻找最大值(1<<k-1为k个1,i^11111每位全部取反,即为寻找互补)
maxx=max(maxx,dp[i]*dp[((<<k)-)^i]);
}
printf("%d\n",maxx);
}
return ;
}

FZU - 2218 Simple String Problem(状压dp)的更多相关文章

  1. FZU - 2218 Simple String Problem 状压dp

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

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

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

  3. CF11D A Simple Task(状压DP)

    \(solution:\) 思路大家应该都懂: 状压DP:\(f[i][j]\),其中 \(i\) 这一维是需要状压的,用来记录19个节点每一个是否已经走过(走过为 \(1\) ,没走为 \(0\) ...

  4. CF11D-A Simple Task【状压dp】

    正题 题目链接:https://www.luogu.com.cn/problem/CF11D 题目大意 给出\(n\)个点\(m\)条边的一张简单无向图,求它的简单环的个数. \(1\leq n\le ...

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

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

  6. cf 11D A Simple Task(状压DP)

    题意: N个点构成的无向图,M条边描述这个无向图. 问这个无向图中共有多少个环. (1 ≤ n ≤ 19, 0 ≤ m) 思路: 例子: 4 6 1 2 1 3 1 4 2 3 2 4 3 4 答案: ...

  7. FZU 1025 状压dp 摆砖块

    云峰菌曾经提到过的黄老师过去讲课时的摆砖块 那时百度了一下题目 想了想并没有想好怎么dp 就扔了 这两天想补动态规划知识 就去FZU做专题 然后又碰到了 就认真的想并且去做了 dp思想都在代码注释里 ...

  8. zoj3777 Problem Arrangement(状压dp,思路赞)

    The 11th Zhejiang Provincial Collegiate Programming Contest is coming! As a problem setter, Edward i ...

  9. poj3311 TSP经典状压dp(Traveling Saleman Problem)

    题目链接:http://poj.org/problem?id=3311 题意:一个人到一些地方送披萨,要求找到一条路径能够遍历每一个城市后返回出发点,并且路径距离最短.最后输出最短距离即可.注意:每一 ...

随机推荐

  1. 九度OJ 1074:对称平方数 (数字特性)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4804 解决:2173 题目描述: 打印所有不超过n(n<256)的,其平方具有对称性质的数. 如11*11=121 输入: 无任何输 ...

  2. 2017-2018-1 20179209《Linux内核原理与分析》第七周作业

    一.实验 1.1task_struct数据结构 Linux内核通过一个被称为进程描述符的task_struct结构体来管理进程,这个结构体包含了一个进程所需的所有信息.它定义在linux-3.18.6 ...

  3. php总结2——php中的变量、数据类型及转换、运算符、流程控制中的分支结构

    2.1  php中的变量: 定义变量:$变量名称=值: 变量名称:$开头    $之后的第一位必须是字母    $第二位之后可以是字母.数字或者是下划线.习惯上变量名称有实际含义,第二个单词后首字母大 ...

  4. BZOJ4944: [Noi2017]泳池

    BZOJ4944: [Noi2017]泳池 题目背景 久莲是个爱玩的女孩子. 暑假终于到了,久莲决定请她的朋友们来游泳,她打算先在她家的私人海滩外圈一块长方形的海域作为游泳场. 然而大海里有着各种各样 ...

  5. 3款Linux网络监视工具

    1 iftop: 如果你想看到现在你的带宽到底是哪些应用在使用,并且各个应用占据了多少带宽的时候,可以用iftop显示出来.使用的参数如下: -h                  display t ...

  6. python_learn1

    1.python在命令行获取当前的路径. import os os.getcwd() os.chdir(r"C:\Users\szlon\Desktop\pythonexer") ...

  7. API的理解和使用——集合

    集合类型的命令及时间复杂度  区间 命令 功能 时间复杂度  集合内 sadd key element [element ... ]  添加元素 O(k),k是元素个数 srem key elemen ...

  8. mysql忘记root密码或报错:ERROR 1044 (42000): Access denied for user ”@’localhost’ to database ‘xx‘

    有的时候忘记了root密码或其他用户的密码,登录的时候报错:ERROR 1044 (42000): Access denied for user ”@’localhost’ to database ' ...

  9. rails debug

    =debug @thesis config下配置 东西需要重启之后才管用

  10. PHP echo() 函数

    实例 输出文本: <?php echo "Hello world!"; ?> 定义和用法 echo() 函数输出一个或多个字符串. 注释:echo() 函数实际不是一个 ...