uva 10887 - Concatenation of Languages

  注意有空串,题目没有说

 用cin会超时,手写hash过的,用了经典的ELFhash,但貌似故意卡了这种hash,要解决冲突。


/*
author:jxy
lang:C/C++
university:China,Xidian University
**If you need to reprint,please indicate the source**
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define Maxn 9999991
using namespace std;
int first[10000000];
char s[1505*1505][30];
int next[1505*1505];
int na,nb,ans;
char a[1505][11],b[11],tt[21];
int ELFhash(char *str)
{
    long long int hash=0,x=0;
    while(*str)
    {
        hash=(hash<<4)+(*str++);
        if((x=hash&0xF0000000L)!=0)
        {
            hash^=(x>>24);
            hash&=~x;
        }
    }
    return (hash&0x7FFFFFFFL)%Maxn;
}
bool insert(char *str,int now)
{
    int i;
    if(!first[now])
    {
        first[now]=ans;
        return 1;
    }
    for(i=first[now];next[i]!=-1;i=next[i])
    {
        if(strcmp(s[i],str)==0)return 0;
    }
    if(strcmp(s[i],str)==0)return 0;
    next[i]=ans;
    return 1;
}
int main()
{
    int T,i,j,C=0;
    scanf("%d",&T);
    while(T--)
    {
        C++;
        memset(first,0,sizeof(first));
        memset(next,-1,sizeof(next));
        scanf("%d%d",&na,&nb);
        getchar();
        for(i=0;i<na;i++)
            gets(a[i]);
        int t;
        ans=1;
        for(j=0;j<nb;j++)
        {
           gets(b);
          for(i=0;i<na;i++)
          {
              strcpy(tt,a[i]);
              strcat(tt,b);
              strcpy(s[ans],tt);
              t=ELFhash(tt);
              if(insert(tt,t))
                  ans++;
          }
        }
        cout<<"Case "<<C<<": "<<ans-1<<endl;
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值