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;
    }
}
时间: 2024-10-26 13:16:46

uva 10887 - Concatenation of Languages的相关文章

10887 - Concatenation of Languages

点击打开链接 题目意思:   给定两组单词,现在有一个组合单词的方法是把第二组中的每一个都加到第一组每一个单词的后面,求组合后总共的单词数. 解题思路:   先让我喷喷,尼玛看看这句什么意思"Youcan assume that the strings are formed by lower case letters ('a' to'z') only, that they are less than10 characters long and that each string is presen

UVa 10887:Concatenation of Languages

链接: UVa :  http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1828 类型:  哈希表 原题: A language is a set of strings. And the concatenation of two languages is the set of all strings that a

UVa 10340:All in All

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1281 原题: You have devised a new encryption technique which encodes a message by inserting between its characters randomly gener

UVa 10391:Compound Words

题目链接: UVa: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1332 zoj :  http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=825 类型: Hash 原题: You are to find all the two-word

UVa 12250 Language Detection (water ver.)

12250 - Language Detection Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=3402 English, Spanish, German, French, Italian and Russian are the 6 most promi

UVa 10157 Expressions:组合数学&amp;amp;高精度

10157 - Expressions Time limit: 10.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=34&page=show_problem&problem=1098 Let X be the set of correctly built parenthesis expressions. The elements of X a

UVa 120 Stacks of Flapjacks (STL deque&amp;amp;reverse)

120 - Stacks of Flapjacks Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=98&page=show_problem&problem=56 Background Stacks and Queues are often considered the bread and butter of dat

UVa 112 Tree Summing (scanf()去空格&amp;amp;递归&amp;amp;二叉树遍历)

112 - Tree Summing Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=48 Background LISP was one of the earliest high-level programming languages and, with FORTRAN, is one o

UVa 10602

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1543 类型:贪心 原题: Company Macrohard has released it's new version of editor Nottoobad, which can understand a few voice commands.