首页 > 其他 > 详细

[哈希]Concatenation of Languages uva10887

时间:2014-02-09 16:13:41      阅读:411      评论:0      收藏:0      [点我收藏+]

Problem A
Concatenation of Languages
Input File: 
Standard Input

Output: Standard Output

 

A language is a set of strings. And the concatenation of two languages is the set of all strings that are formed by concatenating the strings of the second language at the end of the strings of the first language.

 

For example, if we have two language A and B such that:

A = {cat, dog, mouse}

B = {rat, bat}

The concatenation of A and B would be:

C = {catrat, catbat, dograt, dogbat, mouserat, mousebat}

 

Given two languages your task is only to count the number of strings in the concatenation of the two languages.

 

Input

There can be multiple test cases. The first line of the input file contains the number of test cases, T (1≤T≤25). Then T test cases follow. The first line of each test case contains two integers, M and N (M,N<1500), the number of strings in each of the languages. Then the next M lines contain the strings of the first language. The N following lines give you the strings of the second language. You can assume that the strings are formed by lower case letters (‘a’ to ‘z’) only, that they are less than 10 characters long and that each string is presented in one line without any leading or trailing spaces. The strings in the input languages may not be sorted and there will be no duplicate string.

 

Output

For each of the test cases you need to print one line of output. The output for each test case starts with the serial number of the test case, followed by the number of strings in the concatenation of the second language after the first language.

 

Sample Input                               Output for Sample Input

2

3 2

cat

dog

mouse

rat

bat

1 1

abc

cab

Case 1: 6

Case 2: 1

 


Problem setter: Monirul Hasan

Special Thanks: Shahriar Manzoor


题意:将A集合的词与B集合中的词按要求合成一个词,然后利用哈希表判重。

直接用Map或者set竟然也可以过,只不过是时间较长

使用set的代码,执行时间2.6s:

#include<iostream>
#include<string>
#include<set>

using namespace std;

set<string>vis;
string str1[1510],str2[1510];

int main()
{
    int t,k;
    cin>>t;
    for(k=1;k<=t;k++)
    {
        vis.clear();
        int m,n,i,j;
        cin>>m>>n;
        string str;
        getline(cin,str);
        for(i=0;i<m;i++)
            getline(cin,str1[i]);
        for(i=0;i<n;i++)
            getline(cin,str2[i]);
        for(i=0;i<m;i++)
        {
            for(j=0;j<n;j++)
            {
                str=str1[i]+str2[j];
                if(!vis.count(str)) vis.insert(str);
            }
        }
        cout<<"Case "<<k<<": "<<vis.size()<<endl;
    }
    return 0;
}

哈希函数代码,我到现在哈希函数怎么选择还是非常糊涂。。执行时间0.6s

#include<iostream>
#include<string>
#include<cstring>

using namespace std;

const int maxsize=225010;
int head[maxsize],next[maxsize];
string str1[1510],str2[1510],str3[maxsize];
int num;


int Hash(string str)
{
    int sum=0,seed=101;
    for(int i=0;i<str.size();i++)
        sum=sum*seed+(str[i]+1);
    return (sum & 0x7FFFFFFF) %maxsize;
}

void insert(int pos)
{
    int h=Hash(str3[pos]);
    int u=head[h];
    while(u)
    {
        if(str3[u]==str3[pos])
        {
            return;
        }
        u=next[u];
    }
    next[pos]=head[h];
    head[h]=pos;
    num++;
}

int main()
{
    int t,r;
    cin>>t;
    for(r=1;r<=t;r++)
    {
        int m,n,i,j;
        num=1;
        memset(head,0,sizeof(head));
        cin>>m>>n;
        string str;
        getline(cin,str);
        for(i=0;i<m;i++)
            getline(cin,str1[i]);
        for(i=0;i<n;i++)
            getline(cin,str2[i]);
        for(i=0;i<m;i++)
        {
            for(j=0;j<n;j++)
            {
                str3[num]=str1[i]+str2[j];
                insert(num);
            }
        }
        cout<<"Case "<<r<<": "<<num-1<<endl;
    }
    return 0;
}


[哈希]Concatenation of Languages uva10887

原文:http://blog.csdn.net/zju_ziqin/article/details/18992241

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!