uva 10887 Concatenation of Languages(STL set)

本博客介绍如何计算两个语言集合的组合字符串数量,包括输入解析、集合操作和输出格式化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 


题目大意:给出一些句子,找出有多少种句子可以由给出的任意的两个组成,输出个数,要排除重复的。

解题思路:STL中的set,有去重的功能,注意输入的是句子,可能有空格,所以要用gets读取。

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

const int N = 1505;
const int M = 105;
char p[N][M], q[N][M], str[N];

int main(){
    set<string> vec;
    int cas = 1, n, m, sum, t;
    scanf("%d", &sum);
    while (sum--) {
	memset(p, 0, sizeof(p));
	memset(q, 0, sizeof(q));

	scanf("%d%d%*c", &n, &m);
	vec.clear();
	for (int i = 0; i < n; i++)
	    gets(p[i]);
	for (int i = 0; i < m; i++)
	    gets(q[i]);

	for (int i = 0; i < n; i++) {
	    t = strlen(p[i]);
	    for (int j = 0; j < m; j++) {
		strcpy(str, p[i]);
		strcpy(str + t, q[j]);
		vec.insert(str);
	    }
	}
	cout << "Case " << cas++ << ": " << vec.size() << endl;
    }
    return 0;
}

### Concatenation in Programming Languages Usage and Examples In programming, concatenation refers to the operation of joining two strings together end-to-end. This fundamental concept applies across various programming languages but may differ slightly based on syntax and functionality. #### Python Example Python provides simple methods for string concatenation using either the `+` operator or f-strings introduced in version 3.6. ```python first_string = "Hello" second_string = "World" concatenated_result_plus_operator = first_string + ", " + second_string + "!" print(concatenated_result_plus_operator) # Using an f-string for cleaner code concatenated_result_fstring = f"{first_string}, {second_string}!" print(concatenated_result_fstring) ``` #### JavaScript Example JavaScript supports multiple ways to concatenate strings including template literals which are similar to Python's f-strings as well as traditional plus operators. ```javascript let firstName = "John"; let lastName = "Doe"; // Traditional method console.log(firstName + " " + lastName); // Template literal approach console.log(`${firstName} ${lastName}`); ``` #### Java Example Java uses the `+` symbol for basic string concatenations within expressions or statements. For better performance especially when dealing with many operations, StringBuilder class should be considered instead. ```java public class Main { public static void main(String[] args) { String greeting = "Good morning "; String name = "Alice."; System.out.println(greeting + name); } } ``` Concatenation plays a crucial role not just limited to combining textual data but also extends into areas like file path creation, URL generation among others where pieces need assembling dynamically at runtime [^1].
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值