URAL 1577. E-mail(簡單二維dp)
給你兩個子串,讓你找出來一個最短的字串包含這兩個子串,輸出最多的子串有多少種。
類似於最長公共子序列,相等的話長度+1,不想等的話比較長度,使用長度小的。
1577. E-mail
Time limit: 1.0 second
Memory limit: 64 MB
Memory limit: 64 MB
Vasya started to use the Internet not so long ago, so he has only two e-mail accounts at two different servers. For each of them he has a password, which is a non-empty string consisting of only lowercase
latin letters. Both mail servers accept a string as a password if and only if the real password is its subsequence.
Vasya has a hard time memorizing both passwords, so he would like to come up with a single universal password, which both servers would accept. Vasya can't remember too long passwords, hence he is interested
in a universal password of a minimal length. You are to help Vasya to find the number of such passwords.
Input
The input consists of 2 lines, each of them containing the real password for one of the servers. The length of each password doesn't exceed 2000 characters.
Output
Output the number of universal passwords of minimal length modulo 109 + 7.
Samples
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;
}
相關文章
- URAL 1658. Sum of Digits(簡單dp)Git
- URAL 1152 False Mirrors(簡單的狀態壓縮dp)False
- HDU 1227 Fast Food(簡單二維dp)AST
- HDU 5119 Happy Matt Friends(簡單二維dp)APP
- PHP生成簡單二維碼PHP
- 二維碼簡單封裝封裝
- 簡單的Java二維碼應用Java
- 用java做一個簡單的二維碼Java
- phpqrcode生成動態二維碼簡單例項PHP單例
- URAL 1018 Binary Apple Tree(樹形dp入門題)APP
- c#簡單實現二維陣列和二維陣列列表List<>的轉置C#陣列
- 簡單易用的二維碼掃描工具:QR Capture for MacAPTMac
- javascript實現二維陣列實現簡單介紹JavaScript陣列
- 使用 Swift 建立簡單的二維碼掃描應用Swift
- 自定義 React Native 二維碼掃描元件(簡單,易用!)React Native元件
- 簡單的磁碟運維運維
- hdu 1069 Monkey and Banana(簡單dp)NaN
- openjudge1768 最大子矩陣[二維字首和or遞推|DP]矩陣
- 高維字首和(SOS DP)
- ogg簡單維護命令
- 簡單物聯網思維
- [實戰]製作簡單的公眾號二維碼關注圖
- AVFoundation 簡單入門二
- 設定微信和支付寶讚賞二維碼的簡單說明
- HDU 1466 計算直線的交點數(簡單dp)
- 聊聊Dubbo(二):簡單入門
- PLSQL簡單的程式之二SQL
- python簡單爬蟲(二)Python爬蟲
- 簡單分析shared pool(二)
- ASM磁碟簡單維護,新增,刪除ASM
- 決策單調性DP
- Ural2110 : Remove or MaximizeREM
- [dp 小計] wqs 二分
- 簡單的執行緒池(二)執行緒
- 簡單實現二次注入
- Swift-MVVM 簡單演練(二)SwiftMVVM
- WebSocket簡單使用(二)-客戶端Web客戶端
- Android適配:DP簡述Android