跳到正文

个人记录

成都七中 模拟6 - D 题解

lanjiarui约 3 分钟
原文
文章信息与版本

文章信息

发布
2024年4月9日
收录
2026年11月8日
更新
2026年11月15日
篇幅
约 3 分钟

版本

  1. v1当前

题意

根据字串的 Levenshtein 距离,输出 d=0,d=1,d=2,d=3,d>3d=0,d=1,d=2,d=3,d>3 的对应提示词。

思路

RE 50pts:直接 dp
AC100pts:注意到 d>3d>3 的输出一样,则只考虑 dp 中 ∣i−j∣<=4|i-j|<=4 的情况。 复杂度 Θ(n)\Theta(n)。

代码

C++
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
typedef unsigned long long ull;
typedef long long ll;
const ull N=100010,M=100010;
char p[N],s[M];
ull dis()
{
    ull n=strlen(p+1),m=strlen(s+1);
    if(!n||!m) return n+m;
    if(n>=4+m||n+4<=m) return 4;
    static ull _f[N][13];
    auto f=[&](ull i,ull j)->ull&{ return _f[i][i+5-j]; };
    memset(_f,0,sizeof(_f));
    for(ull i(0);i<=5;++i) f(i,0)=i;
    for(ull j(0);j<=5;++j) f(0,j)=j;
    for(ull i(1);i<=n;++i)
    {
        for(ull j(i<=5?1:i-5);j<=m&&j<=i+5;++j)
        {
            if(p[i]==s[j]) f(i,j)=std::min({f(i-1,j)+1,f(i,j-1)+1,f(i-1,j-1)});
            else f(i,j)=std::min({f(i-1,j),f(i,j-1),f(i-1,j-1)})+1;
        }
    }
    return f(n,m);
}
ull T;
int main()
{
    scanf("%llu",&T);
    while(T--)
    {
        scanf("%s%s",p+1,s+1);
        ull d=dis();
        if(d==0) puts("You're logged in!");
        if(d==1) puts("You almost got it. You're wrong in just one spot.");
        if(d==2) puts("You almost got it, but you're wrong in two spots.");
        if(d==3) puts("You're wrong in three spots.");
        if(d>3)  puts("What you entered is too different from the real password.");
    }
    return 0;
}
原文
存档评论1 条抓取于