Gym - 101492L (dp)

Baidu, Inc. is a Chinese Web services company. Among the services they provide are web search and "Baidu Baike", a virtual, collaborative encyclopedia. Due to its remarkable growth and interesting set of challenges its employees need to solve on a daily basis, many young programmers aspire to get a internships or even full-time jobs at this company.

Emi "the Retired" Shouko will travel to Beijing next year and would like to get an interview there for an internship. He is now reading some books with common coding interview questions. He is especially interested in problems related to web search. One of the problems that has been drawing Emi's attention is as follows.

Given an N-character text and an M-character pattern, we would like to know if it is possible to find the pattern in the text. However, the pattern does not need to occur exactly. The search allows a maximum of K mismatches, where a mismatch can be any of the following: a substitution, an insertion, or the removal of a character.

Emi really liked this problem. He has had an idea and started coding his solution furiously. He knows you would love to join him for his trip to Beijing, so he wishes you to solve this problem as well.

Input

The first row of the input has the integers MN, and K. The second row contains a pattern made of M characters from 'a' to 'z'. The third row contains a text made of N characters from 'a' to 'z'.

  • 1 ≤ M ≤ 102
  • 1 ≤ N ≤ 106
  • 1 ≤ K ≤ 20

Output

If the pattern occurs in the text with at most K mismatches, print "S" (without the double quotes). Otherwise, print "N" (without the double quotes).

Examples

Input

题意: 
一个子串pattern在母串中如果能被找到,说明两个串可以匹配,如果不能找到pattern,但是通过最多k个字母的替换、删除、插入可以匹配,则两个串也称为可以匹配。 
输入m,n,k其中m表示pattern s的长度,n表示母串t的长度。

代码:

//Full of love and hope for life

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <queue>
#include <vector>
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
//https://paste.ubuntu.com/
//https://www.cnblogs.com/zzrturist/    //博客园
//https://blog.csdn.net/qq_44134712     //csdn

using namespace std;
const int N=5e4+10;
typedef long long ll;
const int mod=1e9+7;

char s[110];
char ss[1000010];
int dp[1000010][110];//dp[i][j]表示ss[1-i]和s[1-j]能匹配需要的最小的k值 

int main(){
    int a,b,c;
    cin >> a >> b >> c;
    memset(dp,125,sizeof(dp));
    scanf("%s",s+1);
    scanf("%s",ss+1);
    //如果模式串s是空串,则k为0 
    for(int i=0;i<=b;i++){
        dp[i][0]=0;
    }
    for(int i=1;i<=b;i++){
        for(int j=1;j<=a;j++){
            //dp[i][j]的可能取值
            //1、s中前j-1个和ss中前i-1个匹配后,s[j]和ss[i]直接匹配 
            if(ss[i]==s[j]){
                dp[i][j]=min(dp[i-1][j-1],dp[i][j]);
            }
            else{
                dp[i][j]=min(dp[i][j-1]+1,dp[i][j]);
                //2、s中前j-1个和ss中前i在,再在ss中添加s[j] 
                dp[i][j]=min(dp[i-1][j-1]+1,dp[i][j]);
                //3、s中前j个和t中前i-1个匹配后,再删除ss中的第i个 
                dp[i][j]=min(dp[i-1][j]+1,dp[i][j]);
            }
        }
    }
    for(int i=1;i<=b;i++){
        if(dp[i][a]<=c){
            cout << "S";
            return 0;
        }
    }
    cout << "N";
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZZ --瑞 hopeACMer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值