您的位置:首页 > 其它

UVA 10887 Concatenation of Languages

2013-07-31 10:11 405 查看
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 10characters 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

 

题意:输入n,m,然后输入n个字符串和m个字符串。。。然后n个字符串分别和m个字符串拼接(一定是n在前m在后,求出最后拼出来不同的字符串数量)
直接暴力,拼接完的插入到set。。最后输出set。size()..

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <set>
using namespace std;

int t;
int n, m;
char a[1555][15];
char b[1555][15];
set<string> adj;
int main()
{
scanf("%d", &t);
int tt = 1;
while (t --)
{
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
adj.clear();
scanf("%d%d", &n, &m);
getchar();
for (int i = 0 ;i < n; i ++)
gets(a[i]);
for (int i = 0; i < m; i ++)
gets(b[i]);
for (int i = 0; i < n ; i ++)
{
for (int j = 0; j < m; j ++)
{
char c[55];
strcpy(c, a[i]);
strcpy(c + strlen(a[i]), b[j]);
adj.insert(c);
}
}
printf("Case %d: ", tt ++);
cout << adj.size() << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: