HDU 6774 String Distance(2020杭电多校训练第二场)序列自动机+DP

String Distance
Time Limit: 4000/4000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 879 Accepted Submission(s): 310

Problem Description
For two strings S and T, you can do the following operation for arbitrary number of times: Select a string S or T, insert or delete a character at any position. The distance between two strings S and T is defined as the minimum number of operations to make S and T equal.

You will be given two strings A[1…n],B[1…m] and q queries.

In each query, you will be given two integers li and ri (1≤li≤ri≤n), you need to find the distance between the continous substring A[li…ri] and the whole string B.

Input
The first line of the input contains a single integer T (1≤T≤10), the number of test cases.

For each case, the first line of the input contains a string A consists of n (1≤n≤100000) lower-case English letters.

The second line of the input contains a string B consists of m (1≤m≤20) lower-case English letters.

The third line of the input contains a single integer q (1≤q≤100000), denoting the number of queries.

Then in the following q lines, there are two integers li,ri (1≤li≤ri≤n) in each line, denoting a query.

Output
For each query, print a single line containing an integer, denoting the answer.

Sample Input

1
qaqaqwqaqaq
qaqwqaq
3
1 7
2 8
3 9
 

Sample Output

4
2
0

Code:
int nxt[N][50];//a[i…n]中字母j的最近位置
int dp[50][50];//b中前i个字母匹配了j个,dp[i][j]表示最后一个匹配的字母在a中的位置
if(dp[i-1][j]<=r)
dp[i][j]=min(dp[i][j],dp[i-1][j]);//b[j]不匹配
if(dp[i-1][j-1] dp[i][j]=min(dp[i][j],nxt[dp[i-1][j-1]+1][b[i]-‘a’]);//b[j]匹配
这是一个求公共最长子序列很好的板子

#include
#include
#include
#include
#include
#include
#include
#include
#include
#define fors(i, a, b) for(int i = (a); i < (b); ++i)
typedef long long ll;
using namespace std;
const double pi=4*atan(1.0);
const ll mod=1e9+7;
const int inf=0x3f3f3f3f;
#include
using namespace std;
const int N = 123456;
int nxt[N][50];//a[i...n]中字母j的最近位置
int dp[50][50];//b中前i个字母匹配了j个,dp[i][j]表示最后一个匹配的字母在a中的位置
char a[N],b[N];
void get_nxt() //序列自动机
{
    int lena=strlen(a+1);
    for(int i=0; i<26; i++)
        nxt[lena][i]=lena+1;
    nxt[lena][a[lena]-'a']=lena;
    for(int i=lena-1; i>=1; i--)
    {
        for(int j=0; j<26; j++)
        {
            nxt[i][j]=nxt[i+1][j];
        }
        nxt[i][a[i]-'a']=i;
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s%s",a+1,b+1);
        int lenb=strlen(b+1);
        get_nxt();
        int q;
        scanf("%d",&q);
        while(q--)
        {
            int l,r;
            scanf("%d%d",&l,&r);
            memset(dp,0x3f3f3f3f,sizeof(dp));
            dp[0][0]=l-1;
            for(int i=1; i<=lenb; i++)
            {
                dp[i][0]=l-1;
                for(int j=1; j<=i; j++)
                {
                    if(dp[i-1][j]<=r)
                        dp[i][j]=min(dp[i][j],dp[i-1][j]);//b[j]不匹配
                    if(dp[i-1][j-1]

你可能感兴趣的:(2020杭电多校训练)