题目链接:Gym-101492H
动态规划,应该是比较基础的,可是自己就是不会QAQ....
1 /* 把使用机会当成“花费” */ 2 # include <iostream> 3 # include <cstdio> 4 # include <cstring> 5 # include <string> 6 # include <cstdlib> 7 # include <cmath> 8 # include <ctime> 9 # include <climits> 10 # include <memory> 11 # include <functional> 12 # include <algorithm> 13 # include <bitset> 14 # include <set> 15 # include <map> 16 # include <stack> 17 # include <vector> 18 # include <deque> 19 # include <queue> 20 # include <iomanip> 21 # include <utility> 22 using namespace std; 23 24 # define lson l,m,rt<<1 25 # define rson r,m+1,rt<<1|1 26 # define lowbit(x) (x&(-x)) 27 # define lcm(a,b) (a*b/__gcd(a,b)) 28 typedef long long ll; 29 const ll mod=1e9+7; 30 const int maxn=1e6+50; 31 const double pi=acos(-1.0); 32 const int eps=1e-8; 33 34 char s[205], t[1000005]; 35 int dp[maxn][105]; 36 //dp[i][j]表示t[1-i]和t[1-j]能比配需要的最小的k值 37 int main() 38 { 39 int n, m, k; 40 memset(dp, 125, sizeof(dp)); 41 cin>>m>>n>>k; 42 scanf("%s", s+1); 43 scanf("%s", t+1); 44 45 //如果模式串s是空串,则k为0 46 for(int i=0; i<=n; i++ ) 47 dp[i][0] = 0; 48 49 for(int i=1; i<=n; i++ )//t 50 { 51 for(int j=1; j<=m; j++ )//s 52 { 53 //s中前(j-1)个和t中前(i-1)个匹配后,s[j]和t[i]直接匹配 54 if( s[j]==t[i] ) 55 dp[i][j] = min(dp[i-1][j-1], dp[i][j]);//不需要”花费“ 56 57 //在前面已经匹配的基础上,删掉t[i],此时s[j]依旧没有匹配上,所以是dp[i][j-1],而不是dp[i][j] 58 dp[i][j-1] = min(dp[i][j-1], dp[i-1][j-1]+1); 59 //在前面已经匹配的基础上,增加一个t[i]与s[j]匹配,但由于是虚拟的增加,所以仍是dp[i-1][j],而不是dp[i][j]; 60 dp[i-1][j] = min(dp[i-1][j], dp[i-1][j-1]+1); 61 //在前已经匹配的基础上,换t[i] 62 dp[i][j] = min(dp[i][j], dp[i-1][j-1]+1); 63 } 64 } 65 66 for(int i=1; i<=n; i++ ) 67 { 68 if( dp[i][m]<=k ) 69 { 70 cout<<‘S‘<<endl; 71 return 0; 72 } 73 } 74 cout<<‘N‘<<endl; 75 return 0; 76 }