Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- char ar[2010], str[2010];
- bool dp[2010][2010];
- bool DP(int l1,int l2){
- for(int i=l1; i>=0; i--){
- for(int j=l2; j>=0; j--){
- if(i == l1 and j == l2) dp[i][j] = true;
- else if(j == l2) dp[i][j] = false;
- else if(str[j] == '-') dp[i][j] = dp[i+1][j+1];
- else if(str[j] == '$') dp[i][j] = dp[i+1][j+1] or dp[i+1][j] or dp[i][j+1];
- else if(str[j] == ar[i]) dp[i][j] = dp[i+1][j+1];
- else dp[i][j] = false;
- }
- }
- return dp[0][0];
- }
- int main(){
- scanf("%s",ar);
- int n;
- scanf("%d",&n);
- for(int i=0;i<n;i++){
- scanf("%s",str);
- if(DP(strlen(ar),strlen(str))) printf("success\n");
- else printf("fail\n");
- }
- return 0;
- }
- /**
- แบบนี้ก็ได้
- bool DP(int n, int m){
- for(int i=0;i<=n;i++){
- for(int j=0;j<=m;j++){
- if(i == 0 and j == 0) dp[i][j] = true;
- else if(j == 0) dp[i][j] = false;
- else if(i == 0){
- // ระวัง! ในกรณีที่ i = 0, j > 0
- // เช่น str[j] = 'l' -> dp[i][j] = false
- // str[j] = '$' -> dp[i][j] = dp[i][j-1] เพราะว่าสามารถข้ามได้ เช่น str = "$$$" สามารถข้ามได้หมดทุกตัว
- if(str[j-1] == '$') dp[i][j] = dp[i][j-1];
- else dp[i][j] = false;
- }
- else if(str[j-1] == '$') dp[i][j] = dp[i-1][j] or dp[i][j-1] or dp[i-1][j-1];
- else if(str[j-1] == '-') dp[i][j] = dp[i-1][j-1];
- else if(ar[i-1] == str[j-1]) dp[i][j] = dp[i-1][j-1];
- else dp[i][j] = false;
- }
- }
- return dp[n][m];
- }
- **/
Add Comment
Please, Sign In to add comment