uva 10887 - Concatenation of Languages

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

开始将字符串合并后用bkdrhash,一直wrong,20位的字符串hash完后超出long long;然后只有分别对2个子串hash,这样后来要比较2个值,最后还是用stl偷懒水过了;
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int cmp(const void *a,const void*b)
{return strcmp((char *)a,(char *)b);  }
char s1[1500][11],s2[1500][11],a[2250010][21],s[21];
int t,sum,ans;
int main()
{int T,i,j,k,l1,l2,n,m;
 scanf("%d",&t);
 for (T=1;T<=t;T++)
 {
  scanf("%d%d",&n,&m);
  getchar();
  sum=0;
  for (i=0;i<n;i++)
  gets(s1[i]);
  for (i=0;i<m;i++)
  gets(s2[i]);
  for (i=0;i<n;i++)
  for (j=0;j<m;j++)
  {strcpy(s,s1[i]);
   l1=strlen(s1[i]);
   l2=strlen(s2[j]);
   for (k=l1;k<l1+l2;k++)
   s[k]=s2[j][k-l1];
   s[l1+l2]='\0';
   strcpy(a[sum],s);
   ++sum;
  }
  qsort(a,sum,sizeof(a[0]),cmp);
  k=0; ans=0;
  while (k<sum)
  {++ans; j=k;
   while ((j+1<sum)&&(strcmp(a[j+1],a[k])==0)) ++j;
   k=j+1;
  }
  printf("Case %d: %d\n",T,ans);
 }
 return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值