FZU-2218 Simple String Problem

Posted luowentao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了FZU-2218 Simple String Problem相关的知识,希望对你有一定的参考价值。

技术分享图片 Problem 2218 Simple String Problem

Accept: 254    Submit: 594
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

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.

技术分享图片 Source

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

 

题意:
给你一个串和两个整数n和k,n表示串的长度,k表示串只有前k个小写字母,问你两个不含相同元素的连续子串的长度的最大乘积。
思路:
状态压缩DP最多16位,第i位的状态表示第i位字母是否存在,
代码:
#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[N];  
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;  
}  

  

以上是关于FZU-2218 Simple String Problem的主要内容,如果未能解决你的问题,请参考以下文章

SDS(Simple Dynamic String)

Simple String Problem(状压dp)

[Coding Made Simple] String Interleaving

Elasticsearch:理解 query_string 和 simple_query_string 查询

Elasticsearch:理解 query_string 和 simple_query_string 查询

使用 Simple Transformers 微调预训练的语言模型