• hdu 5282 Senior's String 两次dp


    题链:http://acm.hdu.edu.cn/showproblem.php?pid=5282

    Senior's String

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 142    Accepted Submission(s): 40


    Problem Description
    Xuejiejie loves strings most. In order to win the favor of her, a young man has two strings X, Y to Xuejiejie. Xuejiejie has never seen such beautiful strings! These days, she is very happy. But Xuejiejie is missish so much, in order to cover up her happiness, she asks the young man a question. In face of Xuejiejie, the young man is flustered. So he asks you for help.

    The question is that :
    Define the L as the length of the longest common subsequence of X and Y.( The subsequence does not need to be continuous
    in the string, and a string of length L has 2L subsequences containing the empty string ). Now Xuejiejie comes up with all subsequences of length L of string X, she wants to know the number of subsequences which is also the subsequence of string Y.
     

    Input
    In the first line there is an integer T, indicates the number of test cases.

    In each case:

    The first line contains string X, a non-empty string consists of lowercase English letters.

    The second line contains string Y, a non-empty string consists of lowercase English letters.

    1|X|,|Y|1000, |X| means the length of X.
     

    Output
    For each test case, output one integer which means the number of subsequences of length L of X which also is the subsequence of string Y modulo 109+7.
     

    Sample Input
    2 a b aa ab
     

    Sample Output
    1 2
     


    中文题意:

    学姐姐很喜欢字符串,所以学弟送给了她两个字符串作为礼物。
    
    两个字符串分别为XY

    她很开心,但在开心之余她还想考考学弟。

    她定义LXY的最长公共子序列的长度(子序列在字符串内不一定连续。一个长度为L的字符串有2L个子序列,包含空子序列)。

    如今学姐姐取出了X的全部长度为L的子序列。并要求学弟回答在这些子序列中,有多少个是Y的子序列。

    由于答案可能很大,所以学弟仅仅须要回答终于答案模109+7


    官方题解:


    wei里存的是Y串的前j个字母中,  26个字母最后出如今第几个(字符串下标+1)



    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    #define MAX_N 1100//再大dp数组会爆的   O(n*m)
    char a[MAX_N],b[MAX_N];
    int dp[MAX_N+1][MAX_N+1];
    int dd[MAX_N+1][MAX_N+1];//b前j个字母中  最后一个 与x[i]
    int wei[MAX_N+1][26];//Y前i个字母中 最后一个j字母在的位置
    __int64 f[MAX_N+1][MAX_N+1];
    
    int mod=1e9+7;
    int main()
    {
    	int n,m,tem,t;
    	scanf("%d",&t);
    	while(t--) 
    	{
    		scanf("%s%s",a,b);
    		memset(dp,0,sizeof(dp)); 
    		n=strlen(a);
    		m=strlen(b);
    		for(int i=0;i<n;i++)
    		{
    			for(int j=0;j<m;j++)
    			{
    				if(a[i]==b[j]) 
    			    	dp[i+1][j+1]=max(dp[i][j]+1,max(dp[i+1][j],dp[i][j+1])); 
    				else 
    					dp[i+1][j+1]=max(dp[i+1][j],dp[i][j+1]); 
    			}
    		}  
    		memset(wei,0,sizeof wei); 
    		for(int i=1;i<=m;i++)
    		{
    			for(int j=0;j<26;j++)
    				wei[i][j]=wei[i-1][j];
    			wei[i][b[i-1]-'a']=i;
    		} 
    		memset(f,0,sizeof f);
    		for(int i=0;i<=n;i++)
    		{
    			for(int j=0;j<=m;j++)
    			{
    				if(dp[i][j]==0)
    				{
    					f[i][j]=1;
    					continue;
    				}
    				if(dp[i-1][j]==dp[i][j]) 
    					f[i][j]=(f[i][j]+f[i-1][j])%mod;
    				int p=wei[j][a[i-1]-'a'];
    				if(p)
    				{
    					if(dp[i-1][p-1]+1==dp[i][j])
    						f[i][j]=(f[i][j]+f[i-1][p-1])%mod;
    				}
    			}
    		}
    
    		printf("%I64d
    ",f[n][m]%mod);
    	}
    	return 0;
    }


  • 相关阅读:
    C#中的 Attribute 与 Python/TypeScript 中的装饰器是同个东西吗
    如何用一个插件解决 Serverless 灰度发布难题?
    Seata 与三大平台携手编程之夏,百万奖金等你来拿
    函数计算异步任务能力介绍 任务触发去重
    混沌工程平台 ChaosBladeBox 新版重磅发布
    消息队列 RabbitMQ 遇上可观测业务链路可视化
    直播预告 | 容器服务 ACK 弹性预测最佳实践
    Serverless 在阿里云函数计算中的实践
    解密函数计算异步任务能力之「任务的状态及生命周期管理」
    可观测|时序数据降采样在Prometheus实践复盘
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/5159879.html
Copyright © 2020-2023  润新知