标签:style blog io color os ar 使用 for sp
给你两个子串,让你找出来一个最短的字符串包含这两个子串,输出最多的子串有多少种。
类似于最长公共子序列,相等的话长度+1,不想等的话比较长度,使用长度小的。
input | output |
---|---|
b ab |
1 |
abcab cba |
4 |
#include <algorithm> #include <iostream> #include <stdlib.h> #include <string.h> #include <iomanip> #include <stdio.h> #include <string> #include <queue> #include <cmath> #include <stack> #include <map> #include <set> #define eps 1e-8 #define M 1000100 #define LL long long //#define LL long long #define INF 0x3f3f3f #define PI 3.1415926535898 #define mod 1000000007 const int maxn = 2010; using namespace std; LL dp[maxn][maxn]; LL len[maxn][maxn]; char str1[maxn]; char str2[maxn]; int main() { while(~scanf("%s %s",str1+1, str2+1)) { int n = strlen(str1+1); int m = strlen(str2+1); memset(dp, 0, sizeof(dp)); memset(len, 0, sizeof(len)); for(int i = 0; i <= max(n, m); i++) { dp[i][0] = 1; dp[0][i] = 1; len[i][0] = i; len[0][i] = i; } for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { if(str1[i] == str2[j]) { dp[i][j] = dp[i-1][j-1]; len[i][j] = len[i-1][j-1]+1; continue; } if(len[i-1][j] > len[i][j-1]) { dp[i][j] = dp[i][j-1]; len[i][j] = len[i][j-1]+1; continue; } if(len[i][j-1] > len[i-1][j]) { dp[i][j] = dp[i-1][j]; len[i][j] = len[i-1][j]+1; continue; } len[i][j] = len[i-1][j]+1; dp[i][j] += dp[i-1][j]+dp[i][j-1]; dp[i][j] %= mod; } } cout<<dp[n][m]<<endl; } return 0; }
标签:style blog io color os ar 使用 for sp
原文地址:http://blog.csdn.net/xu12110501127/article/details/40707373