Simple String Problem FZU - 2218

点击打开链接

字符种类很少 所以可以利用状压 把每个子串都变做一个数 然后记录每个数(子串)对应的最大长度 然后从低状态一路更新到高状态 这样就相当于一个基于状态高低的前缀和 再然后就是枚举每一种状态 对全一状态也就是最高状态进行异或 这样就得到了每一位都与当前枚举状态不同的最高状态 又因为之前已经求得一个状态前缀和 所以直接相乘即可

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define ll long long

int num[100010],pre[100010];
char ch[2010];
int n,m,tot,k;

int main()
{
    int t,i,j,val,ans;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%s",&n,&m,ch);
        tot=(1<<m);
        memset(num,0,sizeof(num));
        for(i=0;i<n;i++)
        {
            val=0;
            for(j=i;j<n;j++)
            {
                val|=(1<<(ch[j]-'a'));
                num[val]=max(num[val],j-i+1);
            }
        }
        memcpy(pre,num,sizeof(num));
        for(i=0;i<tot;i++)
        {
            for(j=0;j<m;j++)
            {
                if(i&(1<<j))
                {
                    num[i]=max(num[i],num[(1<<j)^i]);
                }
            }
        }
        ans=0;
        for(i=0;i<tot;i++)
        {
            ans=max(ans,num[(tot-1)^i]*pre[i]);
        }
        printf("%d\n",ans);
    }
    return 0;
}

阅读更多

Very Simple Problem

08-06

During a preparation of programming contest, its jury is usually faced with many difficult tasks. One of them is to select a problem simple enough to most, if not all, contestants to solve.nnThe difficulty here lies in diverse meanings of the term "simple" amongst the jury members. So, the jury uses the following procedure to reach a consensus: each member weights each proposed problem with a positive integer "complexity rating" (not necessarily different for different problems). The jury member calls "simplest" those problems that he gave the minimum complexity rating, and "hardest" those problems that he gave the maximum complexity rating.nnThe ratings received from all jury members are then compared, and a problem is declared as "very simple", if it was called as "simplest" by more than a half of the jury, and was called as "hardest" by nobody.nnnInputnnThe first line of input contains integers N and P (1 <= N, P <= 100), the number of jury members and the number of problems. The following N lines contain P integers in range from 0 to 1000 each - the complexity ratings.nnnOutputnnOutput must contain an ordered list of "very simple" problem numbers, separated by spaces. If there are no such problems, output must contain a single integer 0 (zero).nnnThis problem contains multiple test cases!nnThe first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.nnThe output format consists of N output blocks. There is a blank line between output blocks.nnSample Inputnn1nn4 4n1 1 1 2n5 900 21 40n10 10 9 10n3 4 3 5nnnSample Outputnn3

没有更多推荐了,返回首页