题面:洛谷
题解:
做法。。。。非常暴力。
因为要求的编辑距离最多只有1,所以我们直接枚举对那个位置(字符)进行操作,进行什么样的操作,加入/修改/删除哪个字符,然后暴力枚举hash判断即可,
#include<bits/stdc++.h>
using namespace std;
#define R register int
#define AC 25
#define ac 10100
#define base 26
#define LL long long
#define us unsigned int n, m, len, ans, cnt, dfn;
int id[ac];
us LL hs[ac], qw[AC], ls[AC], rs[AC];
char s[AC]; void build()
{
scanf("%s", s + ), len = strlen(s + ), ++ cnt;
for(R i = ; i <= len; i ++) hs[cnt] = hs[cnt] * base + s[i];
} void pre()
{
scanf("%d%d", &n, &m);
for(R i = ; i <= n; i ++) build();
sort(hs + , hs + n + );
qw[] = ;
for(R i = ; i <= ; i ++) qw[i] = qw[i - ] * base;
} bool half(us LL x)
{
int l = , r = n, mid;
while(l < r)
{
mid = (l + r) >> ;
if(hs[mid] == x)
{
if(id[mid] == dfn) return false;
id[mid] = dfn; return true;
}
else if(hs[mid] < x) l = mid + ;
else r = mid - ;
}
if(hs[l] != x) return false;
if(id[l] == dfn) return false;
id[l] = dfn; return true;
} bool check()
{
scanf("%s", s + ), len = strlen(s + ), ans = , ++ dfn;
us LL x = ;
memset(rs, , sizeof(rs));//why?????大概是因为下面判断的时候可能用到r[len + 1]吧,而r[len + 1]应该要=0的
for(R i = ; i <= len; i ++) x = x * base + s[i];
if(half(x))
{
printf("-1\n");
return true;
}
for(R i = ; i <= len; i ++) ls[i] = ls[i - ] * base + s[i];
for(R i = ; i <= len; i ++) rs[i] = ls[len] - ls[i - ] * qw[len - i + ];
//for(R i = 1; i <= len; i ++) printf("%lld ", rs[i]);
//printf("\n");
return false;
} void get()
{
us LL x;
for(R i = ; i <= len; i ++)//枚举对哪一位进行操作,
{//如果是插入的话就是在这个位置的后面插入
if(i)//只有i > 0才可以删除和修改
{
for(R j = ; j < ; j ++)//枚举修改成哪个
{
x = ls[i - ] * qw[len - i + ] + 1LL * (j + 'a') * qw[len - i] + rs[i + ];
if(half(x)) ans ++;
}
x = ls[i - ] * qw[len - i] + rs[i + ];
if(half(x)) ans ++;
}
for(R j = ; j < ; j ++)//枚举在后面插入哪个
{
x = ls[i] * qw[len - i + ] + 1LL * (j + 'a') * qw[len - i] + rs[i + ];
if(half(x)) ans ++;
}
}
printf("%d\n", ans);
} void work()
{
for(R i = ; i <= m; i ++)
{
if(check()) continue;
get();
}
} int main()
{
// freopen("in.in", "r", stdin);
pre();
work();
// fclose(stdin);
return ;
}