簡單dp -- Common Subsequence POJ - 1458
題意:
給你s, t兩個字串,求出兩個字串的最長公共子串,子串的原串字元按原串順序組成的。
思路:
dp[i][j]記錄s串第i個字元匹配到t串第j個字元的最長公共子串長度,把s串的字元與t串的字元挨個比較,若s[i] == t[j], 則此時匹配的最長字串為dp[i][j] = dp[i - 1][j - 1] + 1,否則dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])。
code:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 1e3 + 5;
char s[maxn], t[maxn];
int dp[maxn][maxn];
void init(){
memset(dp, 0, sizeof(dp));
}
int main(){
while(cin >> s + 1 >> t + 1){
init();
int len1 = strlen(s + 1);
int len2 = strlen(t + 1);
for(int i = 1; i <= len1; i++)
for(int j = 1; j <= len2; j++)
if(s[i] == t[j]) dp[i][j] = dp[i - 1][j - 1] + 1;
else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
cout << dp[len1][len2] << endl;
}
}
相關文章
- 「暑期訓練」「基礎DP」 Common Subsequence (POJ-1458)
- 最長公共子序列 Longest Common Subsequence
- POJ1330 Nearest Common Ancestors【LCA】REST
- POJ 3267 The Cow Lexicon(dp)
- POJ 3071 Football(概率DP)
- POJ-3061 Subsequence(字首和+二分/尺取)
- POJ1390 Blocks (區間DP)BloC
- 【簡單搜尋】POJ 2251 Dugeon MasterAST
- Making the Grade POJ - 3666(離散化+dp)
- POJ3252Round Numbers(數位dp)
- POJ - 2236 Wireless Network (kuangbin - 簡單搜尋)
- POJ 3259-Wormholes(簡單判負環)Worm
- POJ3087 Shuffle'm Up【簡單模擬】
- poj--1625Censored!+AC自動機上的dp+大數
- Missing Subsequence Sum
- Android適配:DP簡述Android
- 決策單調性DP
- leetcode392. Is SubsequenceLeetCode
- Lintcode 1263. Is Subsequence
- 簡易狀態壓縮DP
- [學習筆記] 單調佇列最佳化DP - DP筆記佇列
- CF1458D Flip and Reverse 題解
- B. Missing Subsequence Sum
- [atcoder 349] [F - Subsequence LCM]
- dp 套 dp(dp of dp)小記
- 單調佇列最佳化 DP佇列
- 673. Number of Longest Increasing Subsequence
- [LeetCode] 727. Minimum Window SubsequenceLeetCode
- Lowest Common Ancestor
- DP套DP
- 決策單調性最佳化DP
- poj 2031
- poj 3461
- [求職 go][成都] dp 的個人簡歷求職Go
- CF1580D Subsequence 題解
- CF163A Substring and Subsequence 題解
- 【Leetcode】1081. Smallest Subsequence of Distinct CharactersLeetCode
- 【Leetcode】1673. Find the Most Competitive SubsequenceLeetCode