哈希

哈希参考博客

常用的进制hash满足一些性质所有可以O(1) 查询子串或者减去某些位置的子串的hash值。

scanf("%d", &n);
scanf("%s", a+1);
p[0] = 1, h[0] = 0;
for(int i = 1; i <= n; i++){
    h[i] = h[i-1]*base + a[i] - 'A';
    p[i] = p[i-1]*base;
}

1.查询子串的hash值

比如ABACD的子串ACD的hash值为h[5]-h[3-1]*p[5-3+1]

int get_hash(int l, int r)
{
        return h[r] - h[l-1] * p[r-l+1];
}

 

2.查询子串再减去子串中的一个字符

int get_s(int l, int r, int x)
//去掉x位置的字符得到的字符串的hash值
{
    return get_hash(l, x-1) * p[r-x] + get_hash(x+1, r);
}

 

3.查询两个子串拼接的hash

int get_s1_s2(int l1, int r1, int l2, int r2){
    return get_hash(l1, r1) * p[r2-l2+1] + get_hash(l2, r2);
}

 

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-1个中的另一个那就把它输出,如果没有符合的就输出NO TYPOS
解题思路:
就是要把这些字符串连起来,这样就只需要一个一维的h[](题目中说了总的字符串加起来不超过1e6),每一个字符都用' '隔开,
记录他的每一个字符串的开头和结尾,然后再暴力删除某一个字符求解就行
#include<bits/stdc++.h>
#include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
typedef long long ll;
using namespace std;
const int maxn=3e6+100;
char a[maxn];
char b[maxn];
map<ll,int>mp;
vector<int>q; 
ll p[maxn];
ll h[maxn];
int base=31;
ll get(ll l,ll r){
    return h[r]-h[l-1]*p[r-l+1];
}
ll get1(ll l,ll r,ll x){
    //去掉x位置的字符得到的字符串的hash值
    return get(l, x-1) * p[r-x] + get(x+1, r);
} 
int main(){
    int n;
    cin>>n;
    a[0]=' ';
    p[0]=1;
    for(int i=1;i<=n;i++){
        scanf("%s",b);
        b[strlen(b)+1]='';
        b[strlen(b)]=' ';
        strcat(a,b);
    } 
    for(int i=0;a[i];i++){
        if(a[i]==' '){
            q.push_back(i);
            if(i!=0){
                mp[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++){//开头和结尾 
            //去掉j这个位置就是q[i]+1到q[i+1]-1; 
//            ll t=h[j-1]*p[q[i+1]-1-j]+get(j+1,q[i+1]-1);    
            ll t=get1(q[i]+1,q[i+1]-1,j); 
            if(mp[t]){
                for(int k=q[i]+1;k<q[i+1];k++){
                    printf("%c",a[k]);
                }
                flag=0;
                cout<<endl;
                break;
            } 
        }
    }
    if(flag){
        printf("NO TYPOS
");
    } 
} 


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.

原文地址:https://www.cnblogs.com/lipu123/p/13673289.html