2020-09-12 字符串哈希

http://icpc.upc.edu.cn/problem.php?cid=2569&pid=6

题目描述

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

解析

字符串哈希

#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
using namespace std;
char a[2000500]={0};
typedef unsigned long long ll;
char b[2000500]={0};
ll h[2000500]={0};
ll p[2000500]={0};
int base=31;
map<ll,int>w;
vector<int>q;
ll get(ll l,ll r)
{
    //cout<<l<<' '<<r<<' '<<h[r]-h[l-1]*p[r-l+1]<<endl;
    return h[r]-h[l-1]*p[r-l+1];
}
int main()
{
    ios::sync_with_stdio(false);
    cout.tie(NULL);
    int n;
    scanf("%d",&n);
    a[0]=' ';
    for(int i=1; i<=n; i++)
    {
        scanf("%s",b);
        b[strlen(b)+1]='\0';
        b[strlen(b)]=' ';
        strcat(a,b);
    }
    p[0]=1;
    for(int i=0;a[i];i++)
    {
        if(a[i]==' ')
        {
            q.push_back(i);
            if(i!=0)
                w[h[i-1]]=1;
        }
        else
        {
            h[i]=h[i-1]*base+a[i]-'a'+1;
        }
        if(i!=0)
        p[i]=p[i-1]*base;
    }
    int flag=1;
    for(int i=0;i<q.size()-1;i++)
    {
        for(int j=q[i]+1;j<q[i+1];j++)
        {
            ll sum2=h[j-1]*p[q[i+1]-j-1]+get(j+1,q[i+1]-1);
            if(w[sum2])
            {
                for(int k=q[i]+1;k<q[i+1];k++)
                {
                    printf("%c",a[k]);
                }
                printf("\n");
                flag=0;
                break;
            }
        }
    }
    if(flag)
    {
        printf("NO TYPOS\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值