Sicily 1825 Nickname

本文介绍了一种使用C++标准模板库(STL)中的map容器来处理字符串的方法。通过实例展示了如何统计不同字符串出现的次数,并实现了不区分大小写的字符串匹配。

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

巧妙利用STL就可以了

#include <map>
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
struct node
{
    string name;
    int times;
    void add()
    {
        times++;
    }
    node(string n)
    {
        name = n;
        times = 1;
    }
};
int main()
{
    int n,t,i;
    string temp;
    int need =0;
    int s1, s2;
    map<string, node>::iterator p;
    map<string, node> map1;
    cin >> t;
    while ( t-- ) {
          cin >> n;
        while(n--)
        {
            cin >> temp;
            for ( i=0;i<temp.length();i++ ) 
            temp[i]=tolower(temp[i]);
            s1 = map1.size();       
            map1.insert(pair<string, node>(temp, node(temp)));
            s2 = map1.size();
            if (s1 == s2)
            {
                p = map1.find(temp);
                p->second.add();
            }
        }
        for (p = map1.begin(); p != map1.end(); p++)
        {
         cout << p->first << " " << p->second.times << endl;
        }
        cout << endl;
        map1.clear();
    }
    //system( "pause" );
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值