题意:找一个串使给出的两个串都是它的子串,要求最短,求出最短长度,以及种类数。

思路:可以想到,当两个子串a,b拥有最长的公共子串为LCS时,那么可以求出的最短的串为lena+lenb-LCS。 那么接下来直接计算转移数就可以了,和平常求LCS的方法一样。DP[i][j][k]代表到选取了i个时已有j个a串的,k个b串的种类数。

  1. /** @Date : 2016-12-09-18.39
  2. * @Author : Lweleth (SoungEarlf@gmail.com)
  3. * @Link : https://github.com/
  4. * @Version :
  5. */
  6. #include<bits/stdc++.h>
  7. #define LL long long
  8. #define PII pair<int ,int>
  9. #define MP(x, y) make_pair((x),(y))
  10. #define fi first
  11. #define se second
  12. #define PB(x) push_back((x))
  13. #define MMG(x) memset((x), -1,sizeof(x))
  14. #define MMF(x) memset((x),0,sizeof(x))
  15. #define MMI(x) memset((x), INF, sizeof(x))
  16. using namespace std;
  17.  
  18. const int INF = 0x3f3f3f3f;
  19. const int N = 1e5+20;
  20. const double eps = 1e-8;
  21.  
  22. char a[110],b[110];
  23. LL dp[110][50][50];
  24. int main()
  25. {
  26. int T;
  27. int cnt = 0;
  28. cin >> T;
  29. while(T--)
  30. {
  31. scanf("%s%s", &a, &b);
  32. int x = strlen(a);
  33. int y = strlen(b);
  34. int ma = 0;
  35. MMF(dp);
  36. for(int i = 1; i <= x; i++ )
  37. {
  38. for(int j = 1; j <= y; j++)
  39. {
  40. if(a[i-1] == b[j-1])
  41. dp[0][i][j] = dp[0][i - 1][j - 1] + 1;
  42. else dp[0][i][j] = max(dp[0][i][j - 1], dp[0][i - 1][j]);
  43. }
  44. }
  45. int z = x + y - dp[0][x][y];
  46. MMF(dp[0]);
  47. dp[0][0][0] = 1;
  48. for(int i = 1; i <= z; i++)
  49. {
  50. for(int j = 0; j <= x; j++)
  51. {
  52. for(int k = 0; k <= y; k++)
  53. {
  54.  
  55. if(j > 0 && k > 0 && a[j-1] == b[k-1])
  56. {
  57. dp[i][j][k] += dp[i - 1][j - 1][k - 1];
  58. }
  59. else
  60. {
  61. if(j > 0)
  62. dp[i][j][k] += dp[i - 1][j - 1][k];
  63. if(k > 0)
  64. dp[i][j][k] += dp[i - 1][j][k - 1];
  65. }
  66. //cout << dp[i][j][k] << endl;
  67. }
  68. }
  69. }
  70. printf("Case %d: %d %lld\n", ++cnt, z, dp[z][x][y]);
  71.  
  72. }
  73. return 0;
  74. }

LightOJ 1013 - Love Calculator LCS的更多相关文章

  1. Light oj 1013 - Love Calculator (LCS变形)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1013 题意: 给你两个字符串,让你构造出一个长度最小的字符串,且它的子序列包含 ...

  2. [light oj 1013] Love Calculator

    1013 - Love Calculator Yes, you are developing a 'Love calculator'. The software would be quite comp ...

  3. (最长公共子序列+推导)Love Calculator (lightOJ 1013)

    http://www.lightoj.com/volume_showproblem.php?problem=1013   Yes, you are developing a 'Love calcula ...

  4. lightoj 1013 dp

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1013 #include <cstdio> #include <cst ...

  5. lightoj 1013

    思路:动态规划.设dp[i][j][k]表示用第一个串的前i隔字符和第二个串的前k隔字符组成长度为i的串的个数,那么:若s1[j+1] == s2[k+1] dp[i+1][j+1][k+1] += ...

  6. Light OJ 1013 Love Calculator(DP)

    题目大意: 给你两个字符串A,B 要求一个最短的字符串C,使得A,B同时为C的子串. 问C最短长度是多少? C有多少种? 题目分析: 做这道题目的时候自己并没有推出来,看了网上的题解. 1.dp[C串 ...

  7. Light OJ Dynamic Programming

    免费做一样新 1004 - Monkey Banana Problem 号码塔 1005 - Rooks 排列 1013 - Love Calculator LCS变形 dp[i][j][k]对于第一 ...

  8. lightoj刷题日记

    提高自己的实力, 也为了证明, 开始板刷lightoj,每天题量>=1: 题目的类型会在这边说明,具体见分页博客: SUM=54; 1000 Greetings from LightOJ [简单 ...

  9. loj 1013(LCS+记忆化搜索)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=25839 思路:第一小问可以很快求出了,两个字符串的长度-LCS,然 ...

随机推荐

  1. quartz入门实例

    一 Quarta介绍 1 Quartz是什么 Quartz就是一个纯 Java 实现的作业调度工具,相当于数据库中的 Job.Windows 的计划任务.Unix/Linux 下的 Cron,但 Qu ...

  2. [leetcode-658-Find K Closest Elements]

    Given a sorted array, two integers k and x, find the k closest elements to x in the array. The resul ...

  3. 使用HTML5制作loading图

    昨天发了一篇使用HTML5 canvas写的时钟的文章,今天发一篇关于使用HTML5制作loading图的文章. <!DOCTYPE html> <html> <head ...

  4. iOS- iOS 和 Android 的后台推送原理各是什么?有什么区别?

    iOS 的推送iOS 在系统级别有一个推送服务程序使用 5223 端口.使用这个端口的协议源于 Jabber 后来发展为 XMPP ,被用于 Gtalk 等 IM 软件中.所以, iOS 的推送,可以 ...

  5. C# 正则表达式 最全的验证类

    ///<summary> ///验证输入的数据是不是正整数 ///</summary> ///<param name="str">传入字符串&l ...

  6. ZOJ 1457 E-Prime Ring Problem

    https://vjudge.net/contest/67836#problem/E A ring is compose of n circles as shown in diagram. Put n ...

  7. Bookmark Sentry – 检查重复、删除死链书签 Chrome扩展

    Bookmark Sentry 的用处,就是 处理重复的收藏夹的死链 . 重复链收藏.具体,请百度. Bookmark Sentry 下载 :  https://files.cnblogs.com/f ...

  8. linux 相关的问题

    1,查找当前目录下的文件名,并重定向到文件t中 ls > t mac 下快速补全目录名快捷键tab

  9. Activiti5工作流笔记三

    组任务 直接指定办理人 流程图如下: import java.util.HashMap; import java.util.List; import java.util.Map; import org ...

  10. 将下载到本地的JAR包手动添加到Maven仓库(转)

    常用Maven仓库网址:http://mvnrepository.com/http://search.maven.org/http://repository.sonatype.org/content/ ...