1819: [Jsoi]word query Electronic dictionary time limit:10 Sec Memory limit:64 MB
submit:1072 solved:361
[Submit] [Status] [Discuss] Description When people look for a word in an English dictionary, they may not know the complete spelling of the word, but only know the approximate spelling of the word, and then people may get into trouble and waste a lot of time trying to find a word. An electronic dictionary with fuzzy query can solve this problem to some extent: the user simply enters a string, and the electronic dictionary returns a few words that are the smallest distance from the word editing for the user to select. The editing distance between string A and string B is to allow the following "edit" actions on a or B string to change A to B or B to a, and a minimum of "edit" to a distance. To delete a letter from a position in a string; Add a letter to a position in the string; replace one letter in one position of the string with another; The JSOI team is developing an electronic dictionary that you need to help the team implement a counting part for the fuzzy query function: For a string to be queried, if it is a word, 1, if it is not a word, the number of words in the dictionary is returned with its editing distance of 1. The first line of input contains two positive integers n (n < = 10,000) and M (M < = 10,000). Next n lines, one string per line, and I + 1 behavior word wi. The word length is between 1 and 20. The next M line, one string per line, and the first i + N + 1 represents a unknown origin string Qi. The unknown origin string length is between 1 and 20. Both WI and Qi are made of lowercase letters, and the file does not contain extra spaces. All words are different, but the query string may be duplicated. Tip: There are 50% data ranges: N < = 1,000,m < = 1,000. Output outputs shall include m-lines, and I act as an integer XI. Xi =-1 denotes qi as a word in the dictionary, otherwise XI represents the number of words with the QI editing distance of 1. Sample Input4 3
Abcd
Abcde
Aabc
abced
Abcd
Abc
ABCDDSample Output-1
2
3
HINT
ABCD appears in the word list, and the editing distance between ABC and the word ABCD and AABC is 1;ABCDD with the word abcd, ABCDE, and abced editing distances of 1.
Source
Jsoi Second round Contest1
Analysis
Even if the complexity of the problem with Trie is hesitant,,, = = It seems that many people write Trie
The following code implements a Trie that can specify a known prefix for querying
=w= don't know why Blog Park copy codeforces topic will be messy version
Compared to Bzoj will not be: Although the user experience is not good, but at least the layout is normal
Code
1#include <cstdio>2#include <iostream>3#include <cstring>4 #defineMAXN 2020005 using namespacestd;6 7 CharS[MAXN];8 inttrie[maxn][ -],cnt;9 intEnd[maxn],tclock =1, n,m;Ten One voidInsertChar*str) { A intlen = strlen (str), pos =0; - for(inti =0; I < len;i++){ - if(!trie[pos][str[i]-'a']) trie[pos][str[i]-'a'] = ++CNT; thepos = trie[pos][str[i]-'a']; -}end[pos] =1; - } - + intFindChar*STR,intK =0,intpos =0){ - intLen =strlen (str); + for(inti =0; I < len;i++){ A if(!trie[pos][str[i]-'a'])return 0; atpos = trie[pos][str[i]-'a']; -}if(k && End[pos])return 1; - if(End[pos] && end[pos]! =Tclock) { -End[pos] =Tclock; - //printf ("Get in #%d\n", POS); - return 1; in } - return 0; to } + - intMain () { thescanf"%d%d",&n,&m); * $ while(n--){Panax Notoginsengscanf"%s", s); - insert (s); the } + A //for (int i = 0;i <= cnt;i++) { the //For (int j = 0;j < 26;j++) { + //printf ("%d", trie[i][j]); - //}cout << Endl; $ // } $ - while(m--){ -scanf"%s", s); the intLen =strlen (s); - Wuyi if(Find (s),1)){ thecout <<-1<<Endl; - Continue; Wu } - Abouttclock++; $ - //ADD - intpos =0, ans =0; - for(inti =0; I <= len;i++){ A for(intj =0; J < -; j + +){ + the if(!trie[pos][j])Continue; - intCNT =Trie[pos][j]; $ if(Find (S+i,0, CNT)) { theans++; the //cout << pos << pos << Endl; the //printf ("a%d%d\n", i,j); the //cout << s+i << Endl; - } in //ans + = find (s+i,0,cnt); the the } About if(!trie[pos][s[i]-'a']) Break; thepos = trie[pos][s[i]-'a']; the } the + //Delete -pos =0; the for(inti =0; I < len;i++){Bayi if(Find (s+i+1,0, POS)) { theans++; the //printf ("-%d\n", I); - } - if(!trie[pos][s[i]-'a']) Break; thepos = trie[pos][s[i]-'a']; the } the the // Change -pos =0; the for(inti =0; I < len;i++){ the for(intj =0; J < -; j + +){ the if(!trie[pos][j])Continue;94 intCNT =Trie[pos][j]; the if(Find (s+i+1,0, CNT)) { theans++; the //printf ("@%d%d\n", i,j);98 } About } - if(!trie[pos][s[i]-'a']) Break;101pos = trie[pos][s[i]-'a'];102 }103 104printf"%d\n", ans); the }106 107 return 0;108}
"Even pain will chase faith."
[Bzoj] 1819: [Jsoi]word query Electronic Dictionary