其实,这道题目的意思简单的。
大概的意思就是,把找到每一个字符串出现的次数,然后,按照字典顺序把字符串和出现的次数依次输出来。。
题目的链接: 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;
}
}