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;
    }
}


目录
相关文章
|
8月前
UVa11565 - Simple Equations
UVa11565 - Simple Equations
35 0
|
8月前
UVa1583 - Digit Generator
UVa1583 - Digit Generator
34 0
|
机器学习/深度学习
[UVa OJ] Longest Common Subsequence
This is the classic LCS problem. Since it only requires you to print the maximum length, the code can be optimized to use only O(m) space (see here).
756 0
|
人工智能 BI 算法
|
机器学习/深度学习
hdu 3658 How many words
点击打开hdu 3658 思路: 递推+矩阵快速幂 分析: 1 题目的意思是在52个英文字母里面选择m个字母组成一个字符串,满足以下两个条件。
778 0
uva 1121 - Subsequence
点击打开链接uva 1121 思路:二分查找 分析: 1 题目要求找到一个最短的子序列长度并且这个子序列的和大于等于给定的s 2 如果按照常规的做法枚举起点和终点的话肯定是会超时的。
910 0
uva 10317 Equating Equations
点击打开链接uva 10317 思路:搜索 分析: 1 给定一个等式判断两边是否相等,如果一个等式相等那么通过移项到同一边可以得到正数的和等于负数 2 那么通过分析1我们可以知道我们可以求出这个等式的所有数字的和,判断和是否为偶数。
736 0