---
title: "sicily1825. Nickname "
author: "linxiaoty"
date: "2013-05-28T13:40:29Z"
tags: ["C++", "程序设计", "算法"]
categories: ["编程语言"]
slug: "sicily1825-nickname"
draft: false
lastmod: "2013-05-28T13:40:29Z"
id: 1876238
url: "https://linxiaoty.iteye.com/blog/1876238"
---

其实，这道题目的意思简单的。

        大概的意思就是，把找到每一个字符串出现的次数，然后，按照字典顺序把字符串和出现的次数依次输出来。。

        题目的链接： [http://soj.me/1825](http://soj.me/1825)

        下面是我的代码，还有优化的需要，通过的时间为2.3S，题目限定的时间为3S

 

```
#include <iostream>
#include <string>
#include <algorithm>
#include <stdio.h>
#include <cctype>
using namespace std;

string fun(string s)
{
    int len = s.size();
    for (int i = 0; i < len; ++ i)
    {
        s[i] = tolower(s[i]);
    }
    return s;
}


int main()
{
    int t;
    scanf("%d", &t);
    for (int j = 0; j < t; ++ j)
    {
        int n;
        scanf("%d", &n);
        string str[n];
        for (int i = 0; i < n; ++ i)
        {
            cin >> str[i];
            str[i] = fun(str[i]);
        }
        sort(str, str + n);
        int sum = 0;
        string tem = str[0];
        for (int i = 0; i < n; ++ i)
        {
            if (tem == str[i])
            {
                ++ sum;
            }
            else
            {
                cout << tem << " " << sum << endl;
                sum = 1;
                tem = str[i];
            }
        }
        if (sum != 0)
        {
            cout << tem << " " << sum << endl; 
        }
        cout << endl;
    }
}                                 
```