【问题描述】
或许你还没发现,山山有一本古老的词典。
而且他说的每一个单词必然存在于这本字典中。可是由于听力问题,你听到的单词会夹杂着
一些不该有的发音。
例如听到的:
somutchmoreaweare
可能是:
so much more aware
山山的词典里有 n 个单词,你听到一句长度为 m 的话
请你判断这句话最少有几个杂音
【输入文件】
输入文件 dictionary.in 的第 1 行包含 2 个正整数 n,m
第 2 行包含一个长度为 m 的小写字符串 s
接下来 n 行,每行是一个小写单词 ti
【输出文件】
输出文件 dictionary.out 仅包含一个正整数数,最少的杂音数
【样例输入】
5 17
somutchmoreaweare
so
much
more
aware
numb
【样例输出】
2
【数据规模】
对于前 20%的数据 n≤20,m≤20,|ti|≤20
对于前 40%的数据 n≤100,m≤100,|ti|≤100
对于 100%的数据 n≤600,m≤300,|ti|≤300

dp[i]表示匹配到 i 最少含几个杂音
dp[i]可以更新后面的 dp[j],倒着更新效果更好。
Dp[i] = min(dp[i] , dp[i-len[j]-t] + t) 表示 i 后面的字符串匹配第 j 个单词有 t 个杂音,t 可
以用一个匹配函数求,这里用trie+dfs。

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 int ch[180001][27],pos,n,m,f[180001],val[180001];
 7 char s[180001],word[1800001];
 8 void insert(int len)
 9 {
10   int x=0,i;
11   for (i=len-1;i>=0;i--)
12     {
13       if (ch[x][word[i]-'a']==0) ch[x][word[i]-'a']=++pos;
14       x=ch[x][word[i]-'a'];
15     }
16   val[x]=1;
17 }
18 void query(int len,int &p,int x,int cnt)
19 {
20   if (cnt>=p) return;
21   if (val[x]) p=min(p,f[len]+cnt);
22   if (len==0) return;
23   if (ch[x][s[len]-'a']) query(len-1,p,ch[x][s[len]-'a'],cnt);
24   query(len-1,p,x,cnt+1);
25 }
26 int main()
27 {int i;
28   freopen("dictionary.in","r",stdin);
29   freopen("dictionary.out","w",stdout);
30   cin>>n>>m;
31   cin>>s+1;
32   for (i=1;i<=n;i++)
33     {
34       scanf("%s",word);
35       insert(strlen(word));
36     }
37   for (i=1;i<=m;i++)
38     {
39       f[i]=f[i-1]+1;
40       query(i,f[i],0,0);
41     }
42   cout<<f[m];
43 }
View Code

相关文章:

  • 2021-04-10
  • 2021-06-15
  • 2021-08-15
  • 2022-01-07
  • 2021-09-20
  • 2022-12-23
  • 2022-12-23
  • 2021-05-28
猜你喜欢
  • 2021-11-22
  • 2021-05-17
  • 2022-12-23
  • 2021-09-16
  • 2022-12-23
相关资源
相似解决方案