字符串哈希与哈希表练习

练习一:洛谷P1102 A-B 数对 https://www.luogu.com.cn/problem/P1102

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,c;
long long tot=0;
long long  a[200020];
int main()
{
    cin>>n>>c;
    for(int i=1;i<=n;i++)
    cin>>a[i];
    sort(a+1,a+1+n);
    for(int i=1;i<=n;i++)
    {
        tot+=upper_bound(a+1,a+1+n,a[i]+c)-lower_bound(a+1,a+1+n,a[i]+c);

    }
    cout<<tot<<endl;
    return 0;    
}

练习二:洛谷 P1955 [NOI2015]程序自动分析 https://www.luogu.com.cn/problem/P1955

#include<iostream>
#include<cstdio>
#include<bits/stdc++.h>
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <cstdlib>
using namespace std;

int t,n,f[1020000],book[1020000*3];
struct node{
    int x,y,e;
}a[1000001];
bool cmp(node a,node b)
{
    return a.e >b.e ;
}

inline void first(int kkk)
{
    for(int i=1;i<=kkk;i++)
        f[i]=i;//初始化父亲 
}

int find(int x)
{
    if(x==f[x]) return x;
    else return  f[x]=find(f[x]);
}

int main()
{
    cin>>t;
    while(t--)
    {
        int tot=-1;
        memset(book,0,sizeof(book));
        memset(a,0,sizeof(a));
        memset(f,0,sizeof(f));
        int p=1;
        cin>>n;
        for(int i=1;i<=n;i++){
            cin>>a[i].x >>a[i].y >>a[i].e ;
            book[++tot]=a[i].x ;
            book[++tot]=a[i].y ;
        }
        sort(book,book+tot);
        int reu=unique(book,book+tot)-book;//tot表示数组元素个数
        //reu表示去重后数组长度
        first(reu);//表示将去重后的数组元素的祖宗都设为自己
        for(int i=1;i<=n;i++)
        {
            a[i].x =lower_bound(book,book+reu,a[i].x )-book;
            //lower_bound返回元素下标 
            //表示把元素离散化成数组下表(缩小元素范围) 
            a[i].y =lower_bound(book,book+reu,a[i].y )-book;
         } 
         
         sort(a+1,a+1+n,cmp);
         for(int i=1;i<=n;i++)
         {
             int r1=find(a[i].x );
             int r2=find(a[i].y );
             if(a[i].e )
             {
                 f[r1]=r2;//也就是让他们存在于一个集合 
             }
             else if(r1==r2)
             {
                 cout<<"NO"<<endl;
                 p=0;
                 break;
             }
         }
         if(p)
         cout<<"YES"<<endl;
    }
    return 0;
}

练习三:洛谷 P3370 【模板】字符串哈希 https://www.luogu.com.cn/problem/P3370

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;

ull a[1020000];
char s[102000];
int n,ans=1;
ull base=131;
ull mod=453211453444520268,prime=55434453440867;

ull hash(char s[])
{
    ull ans=0;
    ull len=strlen(s);
    for(int i=0;i<len;i++)
        ans=(ans*base+(ull)s[i])%mod+prime;
    return ans;
}

int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>s;
        a[i]=hash(s);
    }
    
    sort(a+1,a+n+1);
    
    for(int i=1;i<n;i++)
    {
        if(a[i]!=a[i+1])
        ans++;
    }
    cout<<ans<<endl;
    return 0;
}

练习四:洛谷 P2957 [USACO09OCT]Barn Echoes G https://www.luogu.com.cn/problem/P2957

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

string sl,sr;
int len_min,lenr,lenl,ansl,ansr,ans;

int main()
{
    cin>>sl>>sr;
    lenl=sl.size() ;
    lenr=sr.size() ;
    len_min=min(lenl,lenr);
    for(int i=1;i<=len_min;i++)
    {
        if(sl.substr(0,i)==sr.substr(lenr-i,i)  )//sl.substr(n,m)  表示从n的位置连续截取m个元素 
        ansl=i;
        if(sr.substr(0,i)==sl.substr(lenl-i,i)  )
        ansr=i;
    }
    //因为是从小到大循环元素个数,所以最后存在的值一定是最大值 
    ans=max(ansl,ansr);
    cout<<ans<<endl;
    return 0;
}

——END——

猜你喜欢

转载自www.cnblogs.com/yxr001002/p/13368274.html