FZU 2218 Simple String 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? |
最近,你突然迷上了字串论。下面是一个关于字符串的有趣问题。 给定一个字符串S,长度为n且包含前k个小写字母。 你被要求从S中找出两个非空子串(注意,子串必须是连续的),并且两个子串不共享任何字母。问题来了,两个子串长度乘积的最大值是多少? |
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. |
输入的首行是一个整数T,表示测试样例的数量。1 <= T <= 50。 对于每个测试样例,第一行有2个整数n和k。(1 <= n <= 2000, 1 <= k <= 16)。 第二行有有一个长度为n的字符串,仅由字母表前k个小写字母组成。比如,k=3,组成元素为a,b,和c。 |
Output |
输出 |
For each test case, output the answer of the question. |
对于每个测试样例,输出问题的答案。 |
Sample Input - 输入样例 |
Sample Output - 输出样例 |
4 25 5 abcdeabcdeabcdeabcdeabcde 25 5 aaaaabbbbbcccccdddddeeeee 25 5 adcbadcbedbadedcbacbcadbc 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. |
对于第一个例子,其中一种可行的子串选择方案为"abc"和"de"。 第三个例子选择的子串为"ded"和"cbacbca"。 第四个例子中,我们无法找出两个非空子串,所以答案为0。 |
【题解】
解题思路:枚举,贪心/搜索,状态压缩/转移。
代码构架:枚举,状态压缩/转移,贪心。
枚举:
因为最多只有前16个字母,一共216种状态,可以使用二进制压缩成一个int来记录每种字母出现的状态(当然你要用unsigned short我也不栏你_(:3 」∠)_,不过int明显看着舒服敲着方便。某人:你再说就要离题了啊,喂!)
枚举的时候可以指定开头再叠加状态,也可以按01背包来写,记录下每种状态子串长度的最大值。
贪心/搜索:
假如我们现在选了0001状态的子串,那么秉持着不拿人民一针一线(误)的优(hen)良(tai)作风,只要最后面不是1的我都要了!
因此得到在可选情况下最好的状态1110。
那么问题来了,学挖掘机……哦不,1110的字符串长度可能是0啊,我们要找的可能是1100,1010,***0。那么把***0中的最大长度丢到1110中,就可以得到1110(其实是***0)的最大长度。
使用位运算可以很方便地完成比较。
状态压缩/转移:
大道无言,百态汇流。
【代码 C++】
#include<cstdio>
#include<cstring>
#include<algorithm>
int n, k, bag[ << ], bit[];
int fid(){
memset(bag, , sizeof(bag));
char data[];
gets(data);
int i, len, temp, ed;
for (i = ; i < n; i += ){//枚举
--i;
for (len = , temp = ; i + len < n; ++len){
temp |= bit[data[i + len] - 'a'];
bag[temp] = std::max(bag[temp], len);
}
}
for (i = , ed = bit[k]; i < ed; ++i){//压缩状态
for (temp = ; temp <= k; ++temp){
if (i & bit[temp]) bag[i] = std::max(bag[i], bag[i^bit[temp]]);
}
}
for (i = , len = , --ed; i < ed; ++i){//搜索
len = std::max(len, bag[i] * bag[i^ed]);
}
return len;
}
int main(){
for (int i = , j = ; i < ; ++i, j <<= ) bit[i] = j;
int T;
scanf("%d", &T);
while (T--){
scanf("%d%d", &n, &k), getchar();
printf("%d\n", fid());
}
return ;
}
#include<cstdio>
#include<cstring>
short bag[];
int n, k, letter[];
int fid(){
memset(bag, , );
char data[];
gets(data);
int i, len, temp, ed = << k;
--ed;
for (i = ; i < n; i += ){
--i;
for (len = , temp = ; i + len < n && temp^ed; ++len){
temp |= letter[data[i + len] - 'a'];
if (bag[temp] < len) bag[temp] = len;
}
}
for (i = ; i <= ed; ++i){
for (temp = ; temp <= k; ++temp)
if (i & letter[temp] && bag[i] < bag[i^letter[temp]])
bag[i] = bag[i^letter[temp]];
}
for (i = , len = ; i < ed; ++i){
temp = bag[i] * bag[i^ed];
if (len < temp) len = temp;
}
return len;
}
int main(){
for (n = , k = ; n < ; ++n, k <<= ) letter[n] = k;
int T;
scanf("%d", &T);
while (T--){
scanf("%d%d", &n, &k), getchar();
printf("%d\n", fid());
}
return ;
}
FZU 2218
FZU 2218 Simple String Problem(简单字符串问题)的更多相关文章
- FZU - 2218 Simple String Problem(状压dp)
Simple String Problem Recently, you have found your interest in string theory. Here is an interestin ...
- FZU - 2218 Simple String Problem 状压dp
FZU - 2218Simple String Problem 题目大意:给一个长度为n含有k个不同字母的串,从中挑选出两个连续的子串,要求两个子串中含有不同的字符,问这样的两个子串长度乘积最大是多少 ...
- (比赛)A - Simple String Problem
A - Simple String Problem Time Limit:10000MS Memory Limit:65536KB 64bit IO Format:%lld & ...
- hdu------(1757)A Simple Math Problem(简单矩阵快速幂)
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- FZU 2215 Simple Polynomial Problem(简单多项式问题)
Description 题目描述 You are given an polynomial of x consisting of only addition marks, multiplication ...
- fzu2218 Simple String Problem
Accept: 2 Submit: 16 Time Limit: 2000 mSec Memory Limit : 32768 KB Problem Description Recent ...
- O - String Problem KMP 字符串最小表示法
Give you a string with length N, you can generate N strings by left shifts. For example let consider ...
- HDU 5284 wyh2000 and a string problem(字符串,水)
题意:比如给你一个串,要求判断wyh是不是它的子序列,那么你只需要找一个w,找一个y,再找一个h,使得w在y前面,y在h前面即可.有一天小学生拿着一个串问他“wyh是不是这个串的子序列?”.但是wyh ...
- FZU2218 Simple String Problem(状压DP)
首先,定义S,表示前k个字符出现的集合,用二进制来压缩. 接下来,推出dp1[S],表示集合为S的子串的最长长度. 然后根据dp1[S]再推出dp2[S],表示集合为S或S的子集的子串的最长长度. 最 ...
随机推荐
- ID3DXMesh接口 创建自己的立方体网格
D3DXCreateMeshFVF 首先创建一个ID3DXMesh接口. ID3DXMesh接口的说明可以参数龙书. 这里主要是用代码来讲解: #define VERTEX_FVF (D3DFVF_X ...
- C++11—lambda函数
[1]lambda表达式语法定义 lambda表达式的语法定义如下: [capture] (parameters) mutable ->return-type {statement}; (1) ...
- android 学习随笔十三(网络:多线程下载)
多线程断点续传下载1.多线程:快* 原理:抢占服务器资源* 单线程下载:线程从第0个字节开始下,下到最后一个字节,在本地硬盘的临时文件中从第0个字节开始写,写到最后一个字节,下载完成时,临时文件也写完 ...
- 【jqGrid for ASP.NET MVC Documentation】.学习笔记.4.性能
1 HTML / ViewState 大小 1.1 HTML 大小 jqGrid for ASP.NET MVC 使用最佳的客户端渲染,意味着 HTML gird 的 尺寸是最小的.事实上,只有 gr ...
- SQL基础教程
第一范式: 列仅包含原子值: 没有重复的组. 第二范式: 满足第一范式: 非部分函数依赖.(如果组合键中任何一列值改变,将导致非键列的值需要被更新) 那么,主键是一列(不是组合的)满足第二范式:所有列 ...
- java 面试每日一题
题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半:再落下,求它在 第10次落地时,共经过多少米?第10次反弹多高? import java.util.Scanner; public cl ...
- 【转】java URLConnection从网上下载图片或音乐
try { //根据String形式创建一个URL对象, URL url = new URL("http://www.baidu.com"); //实列一个URLconne ...
- ACM题目————最短路径问题
Description 给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的. Input 输入n,m,点 ...
- python: indentationerror: unexpected indent
以后遇到了IndentationError: unexpected indent你就要知道python编译器是在告诉你“Hi,老兄,你的文件里格式不对了,可能是tab和空格没对齐的问题,你需要检查下t ...
- C类地址
C类地址第1字节.第2字节和第3个字节为网络地址,第4个字节为主机地址.另外第1个字节的前三位固定为110. C类地址范围:192.0.0.1到223.255.255.255.(第1个字节的二进制值前 ...