#P4407. [JSOI2009] 电子字典

    ID: 3327 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>字符串2009各省省选江苏枚举,暴力深度优先搜索,DFS概率论,统计字典树,Trie 树

[JSOI2009] 电子字典

Description

When looking up a word in an English dictionary, people may not know the exact spelling and only know an incorrect approximate spelling. This can be frustrating and waste a lot of time. An electronic dictionary with fuzzy search can help: the user enters a string, and the dictionary returns a few words with the minimum edit distance to that string for the user to choose from.

The edit distance between string aa and string bb is defined as the minimum number of the following editing operations needed to transform aa into bb or bb into aa:

  1. Delete a letter at some position in the string.
  2. Insert a letter at some position in the string.
  3. Replace the letter at some position in the string with another letter.

The JSOI team is developing an electronic dictionary. You need to help implement a counting module for the fuzzy search function: for a query string, if it is a word, return 1-1; if it is not a word, return how many words in the dictionary have edit distance 11 from it.

Input Format

The first line contains two positive integers NN and MM.

The next NN lines each contain one string; the (i+1)(i+1)-th line is the word WiW_i, with length from 11 to 2020.

Then the next MM lines each contain one string; the (i+N+1)(i+N+1)-th line is the query string QiQ_i. The length of each query string is from 11 to 2020. Each of WiW_i and QiQ_i consists of lowercase letters. There are no extra spaces in the input.

Output Format

Output MM lines. The ii-th line contains an integer XiX_i:

  • Xi=1X_i = -1 means QiQ_i is a word in the dictionary.
  • Otherwise, XiX_i is the number of dictionary words whose edit distance from QiQ_i is 11.
4 3
abcd
abcde
aabc
abced
abcd
abc
abcdd
-1
2
3

Hint

Sample explanation:

  • abcd appears in the word list.
  • The edit distance between abc and each of abcd and aabc is 11.
  • The edit distance between abcdd and each of abcd, abcde, and abced is 11.

Constraints and Notes:

  • All words are distinct, but query strings may repeat.
  • For 50%50\% of the constraints, N,M103N, M \le 10^3.
  • For 100%100\% of the constraints, N,M104N, M \le 10^4.

Translated by ChatGPT 5