zoukankan      html  css  js  c++  java
  • fzu2218 Simple String Problem

    Accept: 2    Submit: 16
    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

    425 5abcdeabcdeabcdeabcdeabcde25 5aaaaabbbbbcccccdddddeeeee25 5adcbadcbedbadedcbacbcadbc3 2aaa

     Sample Output

    6150210

     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种,让你找到两个不同的连续子串,这两个子串满足没有重复的元素种类,然后求符合条件的两个字符串的长度的乘积。
    思路:用b[state]表示字母状态为state的字母种类的最大长度是多少,然后再求dp[state]表示字母种类状态为state及其子集的最大长度,然后就可以用dp[state]*dp[((1<<k)-1)^state]更新答案了。

    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    #include<vector>
    #include<map>
    #include<set>
    #include<queue>
    #include<stack>
    #include<string>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    #define inf 99999999
    #define pi acos(-1.0)
    #define maxn 2005
    char s[maxn];
    int b[140000],dp[140000];
    int main()
    {
        int n,m,i,j,T,k,state,state1,num;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d%d",&n,&k);
            scanf("%s",s+1);
            memset(b,0,sizeof(b));
            for(i=1;i<=n;i++){
                state=0;
                for(j=i;j<=n;j++){
                    state=state|( 1<<(s[j]-'a') ) ;
                    b[state]=max(b[state],j-i+1);
                }
            }
            dp[0]=0;
            for(state=1;state<=(1<<k)-1;state++){
                dp[state]=b[state];
                for(j=1;j<=k;j++){
                    if(state&(1<<(j-1)) ){
                        state1=state-(1<<(j-1));
                        dp[state]=max(dp[state],dp[state1]);
                    }
                }
            }
            num=0;
            for(state=1;state<=(1<<k)-1;state++){
                state1=((1<<k)-1)^state;
                num=max(num,dp[state]*dp[state1]);
    
            }
            printf("%d
    ",num);
        }
        return 0;
    }
    


  • 相关阅读:
    破解Excel写密码保护方法
    【收藏推荐】JavaScript 秘密花园
    Flask框架第六篇.Flask中的蓝图和特殊装饰器
    Flask框架第四篇.Flask 中的 请求Request和session
    Flask框架第三篇.Flask 中的 Response
    Flask框架第八篇.请求上下文
    Flask框架第一篇.Flask框架基础
    Flask框架第二篇.Flask中的配置
    Flask框架第五篇.Flask 中的路由
    Flask框架第七篇.CBV和Flasksession
  • 原文地址:https://www.cnblogs.com/herumw/p/9464587.html
Copyright © 2011-2022 走看看