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

  1. 4
  2. 25 5
  3. abcdeabcdeabcdeabcdeabcde
  4. 25 5
  5. aaaaabbbbbcccccdddddeeeee
  6. 25 5
  7. adcbadcbedbadedcbacbcadbc
  8. 3 2
  9. aaa

Sample Output

  1. 6
  2. 150
  3. 21
  4. 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。详见代码。

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

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. cocos2d-js添加360广告联盟插屏(通过jsb反射机制)

    1.添加demo里的libs里的jar包 2.修改AndroidManifest.xml文件 添加权限: <uses-permission android:name="android. ...

  2. 【题解】CF45G Prime Problem

    [题解]CF45G Prime Problem 哥德巴赫板子题? \(\frac{n(n+1)}{2}\)若是质数,则不需要分了. 上式 若是奇数,那么拆成2和另一个数. 上式 若是偶数吗,直接\(O ...

  3. Java反射详解(转)

    原文地址:http://www.importnew.com/17616.html 动态语言 动态语言,是指程序在运行时可以改变其结构:新的函数可以被引进,已有的函数可以被删除等在结构上的变化.比如众所 ...

  4. Machine Learning No.10: Anomaly detection

    1. Algorithm 2. evaluating an anomaly detection system 3. anomaly detection vs supervised learning 4 ...

  5. Java多线程系列 基础篇06 synchronized(同步锁)

    转载 http://www.cnblogs.com/paddix/ 作者:liuxiaopeng http://www.infoq.com/cn/articles/java-se-16-synchro ...

  6. python 安装coreml

    2.安装pip,  下载get-pip.py, https://bootstrap.pypa.io/get-pip.py,然后Python 这个文件,如果没有权限就加sudo 3.安装coreml:这 ...

  7. 吴恩达机器学习笔记(二) —— Logistic回归

    主要内容: 一.回归与分类 二.Logistic模型即sigmoid function 三.decision boundary 决策边界 四.cost function 代价函数 五.梯度下降 六.自 ...

  8. JS事件派发器EventEmitter

    原文地址:http://zhangyiheng.com/blog/articles/js_event_mitter.html 需求 随着Browser客户端JS越来越复杂,MVC(Client端)设计 ...

  9. Nginx中如何限制某个IP同一时间段的访问次数

    如何设置能限制某个IP某一时间段的访问次数是一个让人头疼的问题,特别面对恶意的ddos攻击的时候.其中CC攻击(Challenge Collapsar)是DDOS(分布式拒绝服务)的一种,也是一种常见 ...

  10. codeforces 569B B. Inventory(水题)

    题目链接: B. Inventory time limit per test 1 second memory limit per test 256 megabytes input standard i ...