当前位置: 首页 > 工具软件 > t-shirts > 使用案例 >

map水题Codehorses T-shirts——CodeForces - 1000A

水麒
2023-12-01

map 由{键,值}对组成的集合,内部实现是一颗红黑树
好啦,具体自行百度。
说起来实在惭愧,map我并不会,只会一个操作就是赋值,还只是一种赋值方法

题目链接:点我跳转

Codehorses has just hosted the second Codehorses Cup. This year, the same as the previous one, organizers are giving T-shirts for the winners.

The valid sizes of T-shirts are either “M” or from 0 to 3 “X” followed
by “S” or “L”. For example, sizes “M”, “XXS”, “L”, “XXXL” are valid
and “XM”, “Z”, “XXXXL” are not.

There are n winners to the cup for both the previous year and the
current year. Ksenia has a list with the T-shirt sizes printed for the
last year cup and is yet to send the new list to the printing office.

Organizers want to distribute the prizes as soon as possible, so now
Ksenia is required not to write the whole list from the scratch but
just make some changes to the list of the previous year. In one second
she can choose arbitrary position in any word and replace its
character with some uppercase Latin letter. Ksenia can’t remove or add
letters in any of the words.

What is the minimal number of seconds Ksenia is required to spend to
change the last year list to the current one?

The lists are unordered. That means, two lists are considered equal if
and only if the number of occurrences of any string is the same in
both lists.

Input

The first line contains one integer n (1≤n≤100) — the number of
T-shirts.

The i-th of the next n lines contains ai — the size of the i-th
T-shirt of the list for the previous year.

The i-th of the next n lines contains bi — the size of the i-th
T-shirt of the list for the current year.

It is guaranteed that all the sizes in the input are valid. It is also
guaranteed that Ksenia can produce list b from the list a.

Output

Print the minimal number of seconds Ksenia is required to spend
to change the last year list to the current one. If the lists are
already equal, print 0.

Input

3
XS
XS
M
XL
S
XS

Output

2

Input

2
XXXL
XXL
XXL
XXXS

Output

1

Input

2
M
XS
XS
M

Output

0

题目大意:给出两份文件对比两个文件中不同的尺寸个数
题中说:T恤的有效尺码为“M”或0至3“X”,后跟“S”或“L”。例如,尺寸“M”,“XXS”,“L”,“XXXL”有效,“XM”,“Z”,“XXXXL”不是。但是这句话好像没什么用啊……
思路:用map记录前n行(第一个文件中)每一行对应的尺寸的个数(即尺寸对应的尺寸个数),然后再查找是否能对应的上。
如果能的话,个数减一,不能的话答案加一;

#include<bits/stdc++.h>
using namespace std;
int main(){
    int a;
    map<string,int>m;//string->int
    while(~scanf("%d",&a)){
        for(int i=0;i<a;i++){
            string s;
            cin>>s;
            m[s]++;//尺寸++
        }
        int ans=0;
        for(int i=0;i<a;i++){
            string s;
            cin>>s;
            if(m[s]==0) ans++;//前一年的文件中没有出现过,ans++
            else m[s]--;//出现过,对应的个数减一
        }
        cout<<ans<<endl;
    }
}

就是一道水题,通过水题掌握一下map最基本的用法,然后再深入学习map吧。

 类似资料: