hdoj1247

哈希算法。各串存入哈希值,再列举各串的断点,O(1)更新哈希值。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <cassert>
#include <stack>
#include <bitset>
//#include <unordered_set>
#define mkp make_pair
#define err cout<<"here"<<endl
using namespace std;
const double EPS=1e-12;
typedef long long lon;
typedef unsigned long long ull;
typedef map<ull,int>::iterator IT;
const lon SZ=50010,SSZ=1010,APB=26,mod=20071027,one=97;
const lon INF=0x7FFFFFFF;
int n;
struct nd{
    lon to,wt;
    nd(lon a=0,lon b=0):to(a),wt(b){}
};
string str[SZ];
ull ft[SZ];
set<ull> st;

void add(int x)
{
    ull hs=0;
    for(int i=0;i<str[x].size();++i)
    {
        hs=hs*one+str[x][i];
    }
    st.insert(hs);
    //cout<<hs<<endl;
}

bool find(ull x)
{
    return st.find(x)!=st.end();
}

bool chk(int x)
{
    ull hs1=0,hs2=0;
    for(int i=0;i<str[x].size();++i)
    {
        hs2=hs2*one+str[x][i];
    }
    int len=str[x].size();
    for(int i=0;i<str[x].size();++i)
    {
        hs1=hs1*one+str[x][i];
        hs2-=ft[len-i-1]*str[x][i];
        if(find(hs1)&&find(hs2))return 1;
    }
    return 0;
}

void init()
{
    for(n=0;cin>>str[++n];)
    {
        add(n);
    }
    ft[0]=1;
    for(int i=1;i<SZ;++i)ft[i]=ft[i-1]*one;
    for(int i=1;i<=n;++i)
    {
        if(chk(i))cout<<str[i]<<endl;
    }
}

void work()
{
    
}

void release()
{    
    //memset(dp,0,sizeof(dp));
}

int main()
{
    std::ios::sync_with_stdio(0);
    //freopen("d:\\1.txt","r",stdin);
    lon casenum;
    //cin>>casenum;
    //cout<<casenum<<endl;
    //for(int time=1;time<=casenum;++time)
    //for(lon time=1;cin>>str+1;++time)
    {
        //if(time!=1)cout<<endl;
        init();
        work();
        release();
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/gaudar/p/10848510.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值