问题 G: Typo
时间限制: 10 S e c 10 Sec 10Sec 内存限制: 128 M B 128 MB 128MB
题目描述
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.
样例输入 Copy
【样例1】
5
hoose
hose
nose
noises
noise
【样例2】
4
hose
hoose
oose
moose
【样例3】
5
banana
bananana
bannanaa
orange
orangers
样例输出 Copy
【样例1】
hoose
noises
noise
【样例2】
hoose
moose
【样例3】
NO TYPOS
题目大意:
给出
n
n
n 个字符串构成的列表,逐个判断每个字符串,如果该字符串删除某个字母后与列表中的某个字符串相同,则输出该字符串,如果所有的字符串都不满足这种情况,则输出“NO TYPOS”.
解题思路
哈希处理每个字符串,对于每个字符串都新开一个哈希数组来存储,然后就是暴力判断删除某个字符后是否与列表中的其他字符串相同。
下面放上一张图来展示这道题所用到哈希的独到之处:
上代码:
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
using namespace std;
typedef unsigned long long ull;
const int N = 1000010;
const int P = 131;
ull p[N];
vector<vector<ull>> h;
unordered_map<ull, bool> mp;
vector<string> v;
int len[1000010];
int main()
{
int n;
cin >> n;
p[0] = 1;
for (int i = 1;i < 1000010;i++) p[i] = p[i - 1] * P;
for (int i = 0;i < n;i++)
{
string str;
cin >> str;
vector<ull> f;
f.push_back(0);
ull sum = 0;
len[i] = str.size();
int m = len[i];
//str = '*' + str;
v.push_back(str);
for (int j = 1;j <= m;j++)
{
f.push_back(f[j - 1] * P + str[j - 1]);
}
mp[f[m]] = true;
h.push_back(f);
}
bool flag = false;
for (int i = 0;i < n;i++)
{
//cout << v[i] << endl;
for (int j = 1;j <= len[i];j++)
{
int r = len[i], l = j + 1;
//cout << l1 << " " << r1 << " " << l2 << " " << r2 << endl;
ull a = h[i][r] - h[i][l - 1] * p[r - l + 1];
ull b = h[i][j - 1] * p[r - l + 1] + a;
if (mp[b])
{
flag = true;
cout << v[i] << endl;
break;
}
}
}
if (!flag) cout << "NO TYPOS" << endl;
return 0;
}