uva 10887 Concatenation of Languages

來源:互聯網
上載者:User

原題:
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
2
3 2
cat
dog
mouse
rat
bat
1 1
abc
cab
Sample Output
Case 1: 6
Case 2: 1

中文:
給你兩組字串A,B,讓你把這兩組串連起來A中的字串在前B中的字串在後面,問你串連後的字串有多少種情況。

#include<bits/stdc++.h>using namespace std;vector<string> A,B,C;int t;int main(){    ios::sync_with_stdio(false);    cin>>t;    int k=1;    while(t--)    {        int m,n;        A.clear();        B.clear();        C.clear();        cin>>m>>n;        cin.ignore();        for(int i=0;i<m;i++)        {            string s;            getline(cin,s);            A.push_back(s);        }        for(int i=0;i<n;i++)        {            string s;            getline(cin,s);            B.push_back(s);        }        for(int i=0;i<m;i++)            for(int j=0;j<n;j++)                C.push_back(A[i]+B[j]);        sort(C.begin(),C.end());        int ans=unique(C.begin(),C.end())-C.begin();        cout<<"Case "<<k++<<": "<<ans<<endl;    }    return 0;}

解答:

我為做這種水題而感到恥辱。。。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.