日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

*【HDU - 5707】Combine String(dp)

發布時間:2023/12/10 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 *【HDU - 5707】Combine String(dp) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

Given three strings?aa,?bb?and?cc, your mission is to check whether?cc?is the combine string of?aa?and?bb.?
A string?cc?is said to be the combine string of?aa?and?bb?if and only if?cc?can be broken into two subsequences, when you read them as a string, one equals to?aa, and the other equals to?bb.?
For example, ``adebcf'' is a combine string of ``abc'' and ``def''.?

Input

Input file contains several test cases (no more than 20). Process to the end of file.?
Each test case contains three strings?aa,?bb?and?cc?(the length of each string is between 1 and 2000).?

Output

For each test case, print ``Yes'', if?cc?is a combine string of?aa?and?bb, otherwise print ``No''.?

Sample Input

abc def adebcf abc def abecdf

Sample Output

Yes No

解題報告:

? 可以直接bool類型的dp[i][j]代表第一個串前i個和第二個串前j個可否匹配的。

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define F first #define S second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 2e5 + 5; char a[MAX],b[MAX],c[MAX]; bool dp[2222][2222]; int main() {while(~scanf("%s%s%s",a+1,b+1,c+1)) {int la = strlen(a+1);int lb = strlen(b+1);int lc = strlen(c+1);if(la+lb!=lc) {puts("No");continue;}memset(dp,0,sizeof dp);dp[0][0]=1;for(int i = 1; i<=la; i++) if(c[i] == a[i]) dp[i][0] |= dp[i-1][0];for(int i = 1; i<=lb; i++) if(c[i] == b[i]) dp[0][i] |= dp[0][i-1]; for(int i = 1; i<=la; i++) {for(int j = 1; j<=lb; j++) {if(c[i+j] == a[i]) dp[i][j] |= dp[i-1][j];if(c[i+j] == b[j]) dp[i][j] |= dp[i][j-1];}}if(dp[la][lb]) puts("Yes");else puts("No");}return 0 ; }

?

但是比賽的時候硬是讓我寫了個序列自動機+dp。

dp[i][j]代表第一個串前i個和第二個串前j個匹配,需要達到的最小位置。

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define F first #define S second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 2000 + 5; char a[MAX],b[MAX],c[MAX]; int dp[MAX][MAX],trie[MAX][222]; int main() {while(~scanf("%s",a+1)) { scanf("%s",b+1);scanf("%s",c+1);memset(dp,0x3f,sizeof dp);dp[0][0]=0;int la = strlen(a+1),lb = strlen(b+1),lc = strlen(c+1); if(la+lb!=lc) {puts("No");continue;}for(int i = 0; i<=129; i++) trie[lc+1][i]=trie[lc+2][i] = lc+1;for(int i = lc; i>=1; i--) {int cur = c[i] ;for(int j = 0; j<129; j++) {if(j == cur) trie[i][j] = i;else trie[i][j] = trie[i+1][j];}}for(int i = 1; i<=la; i++) dp[i][0] = trie[dp[i-1][0]+1][a[i]];for(int i = 1; i<=lb; i++) dp[0][i] = trie[dp[0][i-1]+1][b[i]];for(int i = 1; i<=la; i++) {for(int j = 1; j<=lb; j++) {dp[i][j] = min(dp[i][j],trie[dp[i-1][j]+1][a[i]]);dp[i][j] = min(dp[i][j],trie[dp[i][j-1]+1][b[j]]);}}if(dp[la][lb] != lc) puts("No");else puts("Yes");}return 0 ; }

?

總結

以上是生活随笔為你收集整理的*【HDU - 5707】Combine String(dp)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。