fzuoj 2218 Simple String Problem(状态压缩dp)
2015-12-29 21:14
429 查看
Problem 2218 Simple String Problem
Accept: 10 Submit: 40
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?
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.
For each test case, output the answer of the question.
425 5abcdeabcdeabcdeabcdeabcde25 5aaaaabbbbbcccccdddddeeeee25 5adcbadcbedbadedcbacbcadbc3 2aaa
6150210
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个小写字母,问你两个不含相同元素的连续子串的长度的最大乘积。
二进制状态压缩,每位的状态表示第i个字母存在状态,n^2的时可以枚举出所有子串的状态和长度。然后每次与(1<<k - 1)异或就是不含相同的子串
状态。但是我开始想直接对每一种状态枚举他的子集和异或值,不过这太大了,肯定会超时,不过我们可以用dp[t]表示t状态所有子集的最大长度
先状态转移一下,最后之间遍历所有状态和其异或状态就可以更新出答案。
Accept: 10 Submit: 40
Time Limit: 2000 mSec Memory Limit : 32768 KB
![](http://acm.fzu.edu.cn/image/prodesc.gif)
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?
![](http://acm.fzu.edu.cn/image/prodesc.gif)
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.
![](http://acm.fzu.edu.cn/image/prodesc.gif)
Output
For each test case, output the answer of the question.
![](http://acm.fzu.edu.cn/image/prodesc.gif)
Sample Input
425 5abcdeabcdeabcdeabcdeabcde25 5aaaaabbbbbcccccdddddeeeee25 5adcbadcbedbadedcbacbcadbc3 2aaa
![](http://acm.fzu.edu.cn/image/prodesc.gif)
Sample Output
6150210
![](http://acm.fzu.edu.cn/image/prodesc.gif)
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.
![](http://acm.fzu.edu.cn/image/prodesc.gif)
Source
第六届福建省大学生程序设计竞赛-重现赛(感谢承办方华侨大学)题意:给你一个串和两个整数n和k,n表示串的长度,k表示串只有前k个小写字母,问你两个不含相同元素的连续子串的长度的最大乘积。
二进制状态压缩,每位的状态表示第i个字母存在状态,n^2的时可以枚举出所有子串的状态和长度。然后每次与(1<<k - 1)异或就是不含相同的子串
状态。但是我开始想直接对每一种状态枚举他的子集和异或值,不过这太大了,肯定会超时,不过我们可以用dp[t]表示t状态所有子集的最大长度
先状态转移一下,最后之间遍历所有状态和其异或状态就可以更新出答案。
#include<iostream> #include<algorithm> #include<cstdio> #include<queue> #include<map> #include<vector> #include<cstring> #include<cmath> #define eps 1e-12 using namespace std; typedef long long ll; const ll mo = 1000000007, N = 2*1e3+10; char s ; int dp[(1<<16)+100]; int main() { int t; cin>>t; while(t--) { int n, m; scanf("%d%d", &n, &m); scanf("%s", s); memset(dp, 0, sizeof(dp)); for(int i = 0; i<n; i++) { int t = 0; for(int j = i; j<n; j++) { t |= 1<<(s[j] - 'a'); dp[t] = max(dp[t], j - i + 1); } } int s = 1<<m; for(int i = 0; i<s; i++) { for(int j = 0; j<m; j++) { if((1<<j) & i) dp[i] = max(dp[i], dp[i^(1<<j)]); } } int ans = 0; for(int i = 0; i<s; i++) { ans = max(ans, dp[i]*dp[(s-1)^i]); } cout<<ans<<endl; } return 0; }
相关文章推荐
- 数据库按照手机号分库后,还需要允许用户使用邮箱、用户名等登录,怎么才能提高查询效率?
- tomcat安装
- Docker for Windows安装与Linux+PHP开发环境搭建(二)
- Spring项目启动时执行初始化方法
- 零崎的朋友很多Ⅲ(矩阵链相乘)
- 注解
- 浅谈操作系统对内存的管理
- Java必要基础理解
- Android QQ,微信,新浪微博登录笔记
- Redhat 7修改httpd的默认端口
- 系统崩溃,大圣归来
- 系统崩溃,大圣归来
- Enhancement Framework For Classes
- java的常见的字符字节处理
- 系统崩溃,大圣归来
- [LeetCode] 179. Largest Number 解题思路
- Java中JDBC连接数据库Oracle
- tinyos学习笔记9--TestRPL(UDP)例程分析
- CCNP学习笔记16-MSTP,span
- 技术杂记-日期时间字符串解析识别