HDU5262 15年百度之星复赛1005 最强密码(神奇的dp)(好题)

18 篇文章 0 订阅
4 篇文章 0 订阅

Problem Description

由于近来密码库被盗的现象层出不穷,度度熊决定为自己的账号找一个最强密码。在研究了密码库很久之后,它总结出了一个规律:密码库中的所有密码都是一个“密码生成串”的子序列(某个序列的子序列是从最初序列通过去除某些元素但不破坏余下元素的相对位置(在前或在后)而形成的新序列)。

经过强大的计算集群夜以继日的工作,度度熊得到了这个“密码生成串”。现在它希望找到一个“最强密码”,不是这个“密码生成串”的子序列,并且长度最短。

任性的度度熊还希望知道这样的“最强密码”有多少个。幸运的是,“密码生成串”和“最强密码”都只包含小写字母。

Input

第一行一个整数T,表示T组数据 (1T100) 

每组数据包含一个“密码生成串” (100000) 

Output

对于每组测试数据输出两行:

第一行输出"Case #i:",其中 I 代表第 I 组测试数据。

第二行输出两个数,最短长度和该长度的“最强密码”个数(对 1 000 000 007 取模)。用空格分隔。

Sample Input
2
ab
abcdefghijklmnopqrstuvwxyzzyxwvutsrqponmlkjihgfedcba
Sample Output
Case #1:
1 24
Case #2:
3 6201

Time Limit: 10000/5000 MS (Java/Others)


#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstring>
#include<iostream>
using namespace std;
const int MOD = 1e9+7;
const int SZ = 26;
const int N = 101005;
int L[N], dp[N];
char str[N];
int pos[N][SZ];//pos[i][j]记录在i位置之后的sz字符的第一个位置
int main()
{
    int T;
    int i, j, len;
    int cs = 0;
 
    while(T--){
        scanf("%s",str+1);
        len = strlen(str+1);
        for(i=1; i<=len+1; ++i){
            L[i] = len+1;
        }
        for(i=0; i<SZ; ++i) pos[len][i] = len+1;
        for(i=len; i>=1; --i){
            for(j=0; j<SZ; ++j) pos[i-1][j] = pos[i][j];
            pos[i-1][str[i]-'a'] = i;
        }

        L[0] = 0;
        dp[0] = 1;
        for(i=0; i<=len; ++i){
            for(j=0; j<SZ; ++j){
                int nex = pos[i][j];
                if(L[i]+1 < L[nex]){
                    L[nex] = L[i]+1;
                    dp[nex] = dp[i];
                }else if(L[i]+1 == L[nex]){
                    dp[nex] += dp[i];
                    if(dp[nex] >= MOD) dp[nex] -= MOD;
                }
            }
        }
        printf("Case #%d:\n%d %d\n",++cs,L[len+1], dp[len+1]);
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值