题链: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
  1. 2
  2. a
  3. b
  4. aa
  5. ab
 
Sample Output
  1. 1
  2. 2
 

中文题意:

  1. 学姐姐很喜欢字符串,所以学弟送给了她两个字符串作为礼物。
  2.  
  3. 两个字符串分别为XY
  4.  
  5. 她很开心,但在开心之余她还想考考学弟。
  6.  
  7. 她定义LXY的最长公共子序列的长度(子序列在字符串内不一定连续。一个长度为L的字符串有2L个子序列,包含空子序列)。
  8.  
  9. 如今学姐姐取出了X的全部长度为L的子序列。并要求学弟回答在这些子序列中,有多少个是Y的子序列。
  10.  
  11. 由于答案可能很大,所以学弟仅仅须要回答终于答案模109+7

官方题解:

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

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<algorithm>
  4. using namespace std;
  5. #define MAX_N 1100//再大dp数组会爆的 O(n*m)
  6. char a[MAX_N],b[MAX_N];
  7. int dp[MAX_N+1][MAX_N+1];
  8. int dd[MAX_N+1][MAX_N+1];//b前j个字母中 最后一个 与x[i]
  9. int wei[MAX_N+1][26];//Y前i个字母中 最后一个j字母在的位置
  10. __int64 f[MAX_N+1][MAX_N+1];
  11.  
  12. int mod=1e9+7;
  13. int main()
  14. {
  15. int n,m,tem,t;
  16. scanf("%d",&t);
  17. while(t--)
  18. {
  19. scanf("%s%s",a,b);
  20. memset(dp,0,sizeof(dp));
  21. n=strlen(a);
  22. m=strlen(b);
  23. for(int i=0;i<n;i++)
  24. {
  25. for(int j=0;j<m;j++)
  26. {
  27. if(a[i]==b[j])
  28. dp[i+1][j+1]=max(dp[i][j]+1,max(dp[i+1][j],dp[i][j+1]));
  29. else
  30. dp[i+1][j+1]=max(dp[i+1][j],dp[i][j+1]);
  31. }
  32. }
  33. memset(wei,0,sizeof wei);
  34. for(int i=1;i<=m;i++)
  35. {
  36. for(int j=0;j<26;j++)
  37. wei[i][j]=wei[i-1][j];
  38. wei[i][b[i-1]-'a']=i;
  39. }
  40. memset(f,0,sizeof f);
  41. for(int i=0;i<=n;i++)
  42. {
  43. for(int j=0;j<=m;j++)
  44. {
  45. if(dp[i][j]==0)
  46. {
  47. f[i][j]=1;
  48. continue;
  49. }
  50. if(dp[i-1][j]==dp[i][j])
  51. f[i][j]=(f[i][j]+f[i-1][j])%mod;
  52. int p=wei[j][a[i-1]-'a'];
  53. if(p)
  54. {
  55. if(dp[i-1][p-1]+1==dp[i][j])
  56. f[i][j]=(f[i][j]+f[i-1][p-1])%mod;
  57. }
  58. }
  59. }
  60.  
  61. printf("%I64d\n",f[n][m]%mod);
  62. }
  63. return 0;
  64. }

hdu 5282 Senior&#39;s String 两次dp的更多相关文章

  1. HDU 5280 Senior&#39;s Array 最大区间和

    题意:给定n个数.要求必须将当中某个数改为P,求修改后最大的区间和能够为多少. 水题.枚举每一个区间.假设该区间不改动(即改动该区间以外的数),则就为该区间和,若该区间要改动,由于必须改动,所以肯定是 ...

  2. HDU 5281 Senior&#39;s Gun 杀怪

    题意:给出n把枪和m个怪.每把枪有一个攻击力,每一个怪有一个防御力.假设某把枪的攻击力不小于某个怪的防御力则能将怪秒杀,否则无法杀死.一把枪最多仅仅能杀一个怪,不能用多把枪杀同一个怪.每杀一次怪能够得 ...

  3. HDU 5281 Senior&#39;s Gun

    Senior's Gun Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tot ...

  4. hdu 5030 Rabbit&#39;s String(后缀数组&amp;二分法)

    Rabbit's String Time Limit: 40000/20000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  5. HDU 5280 Senior&#39;s Array

    Senior's Array Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) T ...

  6. hdu 4865 Peter&#39;s Hobby(概率dp)

    http://acm.hdu.edu.cn/showproblem.php? pid=4865 大致题意:有三种天气和四种叶子状态.给出两个表,各自是每种天气下叶子呈现状态的概率和今天天气对明天天气的 ...

  7. 【HDU 3336】Count the string(KMP+DP)

    Problem Description It is well known that AekdyCoin is good at string problems as well as number the ...

  8. hdu 2476"String painter"(区间DP)

    传送门 https://www.cnblogs.com/violet-acmer/p/9852294.html 题意: 给定字符串A,B,每次操作可以将字符串A中区间[ i , j ]的字符变为ch, ...

  9. hdu 4057 AC自己主动机+状态压缩dp

    http://acm.hdu.edu.cn/showproblem.php?pid=4057 Problem Description Dr. X is a biologist, who likes r ...

随机推荐

  1. Windows Azure 安全最佳实践 - 第 5 部分:基于Claim 的标识,单点登录

    基于Claim的身份标识是处理网站与 Web 服务的身份认证和访问一种简单而强大的方式,无论您是在本地工作还是面向云工作.您可以通过减少自定义实施和使用基于Claim的单一简化标识模型,创建更安全的应 ...

  2. Windows Azure 安全最佳实践 - 第 3 部分:确定安全框架

    构建云应用程序时,安全始终是计划和执行Windows Azure的首要核心因素.第 1 部分提出安全是一项共同责任,Windows Azure为您的应用程序提供超出内部部署应用程序需求的强大安全功能. ...

  3. 浅谈sqlldr

    1.安装oracle  sqlldr 2.配置sqlldr环境 3java代码的实现 在windows下面sqlldr: sqlldr = “cmd /c start  D:/oracle/produ ...

  4. EL表达式(1)

    JSP页面中支持使用EL表达式,EL全名为Expression Language.EL表达式的主要作用有: ① 获取数据: ② 执行运算: ③ 使用EL表达式的11大隐式对象: ④ 调用Java方法. ...

  5. 关于iOS7以后版本号企业公布问题

    大家都知道,苹果在公布7.1以后,不打个招呼就把企业公布方式给换掉了(谴责一下~) 曾经普通server+web页面+ipa+plist就能够搞定,如今已经不行了. 关于如今企业公布教程网上贴出来了非 ...

  6. Swift - 异步获取网络数据封装类

    使用NSURLConnection.sendAsynchronousRequest()可以采用异步获取的方式取得数据.下面通过对数据获取类进行封装,演示如何进行数据请求与接收. 1,HttpContr ...

  7. Java反射库中的安全漏洞在30个月后终于修复了(转)

    2013年7月,安全组织Security Explorations发现了Java 7u25中的一个安全漏洞,通过这个漏洞攻击者可以完全摆脱Java沙箱.Oracle在更新的7u40中包含了一个补丁,但 ...

  8. js弹出对话框,遮罩效果,

    刚刚来到实习单位,我跟着廖哥做项目.然后他分配给我一个小小的任务,实现起来总的效果如下: 然后,但我们单击显示数目这个链接的时候,就会弹出一个又遮罩效果的对话框,如下图: 当我们在对话框中再点击里面的 ...

  9. Qt Windows下开机自动启动自己的程序

    源地址:http://blog.csdn.net/chrisfxs/article/details/13279491 版权声明:本文为博主原创文章,未经博主允许不得转载. void Widget::R ...

  10. 使用SetUnhandledExceptionFilter转储程序崩溃时内存DMP注意事项

    使用代码手工生成dmp文件 SetUnhandledExceptionFilter 为每个线程设置SetUnhandledExceptionFilter(MyCallBack),(必须在每个线程中启动 ...