字符串在不在字符串数组里:字符串哈希

20 篇文章 0 订阅
# include <iostream>
# include <stdio.h>
# include <string.h>
# include <algorithm>
# include <math.h>
# include <queue>
# include <string>
# include <vector>
# include <set>
# include <map>
#include<string>
# define INF 0x3f3f3f3f
# define clr0(x) memset(x,0,sizeof(x))
# define clr1(x) memset(x,INF,sizeof(x))
# define clrf(x) memset(x,-1,sizeof(x))
# define rep(i,a) for(int i = 0;i<(a);i++)
# define repf(i,a,b) for(int i = (a);i<=(b);i++)
# define repu(i,a,b) for(int i = (a);i<(b);i++)
# define repd(i,a,b) for(int i = (a);i>=(b);i--)
# define lowbit x&(-x)
# define W(a) while(a)
# define Sc(x) scanf("%d",&(x))
# define Sd(x) scanf("%I64d",&(x))
# define Lson(x) (x)<<1
# define Rson(x) (x)<<1|1
# define ll long long
# define fuckio ios::sync_with_stdio(false);
# define fuck cout<<"fucl:"<<__LINE__<<endl;
# define gcd(a,b) __gcd((a),(b))
# define maxn 100020
using namespace std;

const int ALTN = 30;
int a[maxn];
int nexta[maxn];
void getnxt(string s)
{
    int n = (int)s.length();
    int i = -1;int j = 0;
    clr0(nexta);
    nexta[0] = -1;
    W(j<n){
        if(i == -1||s[i] == s[j]){
            nexta[j+1] = i+1;
            i++;j++;
        }else{
            i = nexta[i];
        }
    }
}

int kmp(string s,string t)
{
    int n = (int)s.length();
    int m = (int)s.length();
    int res = -1;
    int i = 0,j = 0;
    W(i<n&&j<m){
        if(j == -1||s[i] == t[j]){
            i++;j++;
        }else {
            j = nexta[j];
        }
    }
    if(j == m)res = i-m;
    return res;
}


struct Node
{
    Node *nxt[ALTN];
    int flag;
    Node(){
        rep(i,ALTN)nxt[i] = NULL;
        flag = 0;
    }
};
Node *root;
void init()
{
    root  = new Node();
}
void ins(string s)
{
    int len = (int)s.length();
    Node *now = root;
    rep(i,len){
        int to = s[i]-'a';
        if(now->nxt[to] == NULL)now->nxt[to] = new Node();
        now = now->nxt[to];
    }
    now->flag++;
}

int fid(string s)
{
    int len = (int)s.length();
    Node *now = root;
    rep(i,len){
        int to = s[i]-'a';
        if(now->nxt[to] == NULL)return 0;
        now = now->nxt[to];
    }
    return now->flag;
}

int main()
{
    int n;
    scanf("%d",&n);
    string s[50005];
    for(int i=0;i<n;i++)
    {
        cin>>s[i];
    }
    init();
    for(int i=0;i<n;i++)
    {
        ins(s[i]);
    }
    int ans=0;
    for(int i=0;i<n;i++)
    {
        reverse(s[i].begin(), s[i].end());
        if(fid(s[i])==1)
        {
            ans++;
        }
    }
    cout<<ans/2<<endl;
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gy5461

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值