Typo

Typo

时间限制: 10 Sec  内存限制: 128 MB

题目描述

It is now far into the future and human civilization is ancient history. Archaeologists from a distant planet have recently discovered Earth. Among many other things, they want to decipher the English language. 
They have collected many printed documents to form a dictionary, but are aware that sometimes words are not spelled correctly (typos are a universal problem). They want to classify each word in the dictionary as either correct or a typo. Naïvely, they do this using a simple rule: a typo is any word in the dictionary such that deleting a single character from that word produces another word in the dictionary.
Help these alien archaeologists out! Given a dictionary of words, determine which words are typos. That is,which words result in another word in the dictionary after deleting a single character.
For example if our dictionary is {hoose, hose, nose, noises}. Then hoose is a typo because we can obtain hose by deleting a single ’o’ from hoose. But noises is not a typo because deleting any single
character does not result in another word in the dictionary.
However, if our dictionary is {hoose, hose, nose, noises, noise} then the typos are hoose, noises,and noise.

输入

The first line of input contains a single integer n, indicating the number of words in the dictionary.
The next n lines describe the dictionary. The ith of which contains the ith word in the dictionary. Each word consists only of lowercase English letters. All words are unique.
The total length of all strings is at most 1 000 000.

输出

Display the words that are typos in the dictionary. These should be output in the same order they appear in the input. If there are no typos, simply display the phrase NO TYPOS.

样例输入

【样例1】
5
hoose
hose
nose
noises
noise
【样例2】
4
hose
hoose
oose
moose
【样例3】
5
banana
bananana
bannanaa
orange
orangers

样例输出

【样例1】
hoose
noises
noise
【样例2】
hoose
moose
【样例3】
NO TYPOS
 1 #include <map>
 2 #include <vector>
 3 #include <cstdio>
 4 #include <iostream>
 5 #include <algorithm>
 6 using namespace std;
 7 typedef unsigned long long ULL;
 8 const int N = 1000009;
 9 int n, ans;
10 string s;
11 vector<string> g;
12 vector<int> len;
13 vector<vector<ULL> > f;
14 map<ULL, bool> v;
15 vector<ULL> F;
16 ULL p[N];
17 int main()
18 {
19     scanf("%d", &n);
20     p[0] = 1;
21     for(int i = 1; i <= 1000000; i++)
22         p[i] = p[i-1] * 131;
23     for(int i = 1; i <= n; i++)
24     {
25         cin >> s;
26         int m = s.length();
27         g.push_back(s);
28         len.push_back(m);
29         F.clear();
30         F.push_back(0);
31         for(int j = 1; j <= m; j++)
32             F.push_back(F[j-1]*131+(s[j-1]-'a'+1));
33         f.push_back(F);
34         v[F[m]] = 1;
35     }
36     for(int i = 0; i < n; i++)
37     {
38         bool flag = 0;
39         for(int j = 1; j <= len[i]; j++)
40         {
41             int r = len[i], l = j+1;
42             ULL h = f[i][r] - f[i][l-1] * p[r-l+1];
43             ULL H = h + f[i][j-1] * p[r-l+1];
44             if(v[H])
45             {
46                 flag = 1;
47                 break;
48             }
49         }
50         if(flag)
51         {
52             ++ans;
53             cout << g[i] << endl;
54         }
55     }
56     if(!ans) puts("NO TYPOS");
57     return 0;
58 }
View Code

 

 

 

 
posted @ 2020-09-12 09:50  Johnny-English  阅读(261)  评论(0编辑  收藏  举报