1715: URL
Time Limit(Common/Java):1000MS/10000MS Memory Limit:65536KByte
Description
WHU ACM Team is working on a brand new web browser named "Whu-Super-Browser". You're in response for a powerful feature: recording the previous addresses. Moreover, when a string is inputted, the browser will display all the addresses start with it. The addresses shall be sorted by visited times, in descending order. This feature is very useful to users. Can you complete it?
There're two kinds of operations:
Visit [url_str] : visit a website with the URL: [url_str].
Display [str] : display all addresses start with [str] and sort them by visited times, in descending order. If two addresses
have the same visited times, then sort them in the lexicographical order.
Input
The input consists of multiple test cases. The first line of input contains an integer T, which is the number of test cases.
Each test case is on several lines. The first line of each test case consists of an integer N. Each of the following N lines consists of an operation, Visit or Display.
[Technical Specification]
T is an integer, and T <= 10.
N is an integer, and 1 <= N <=100.
There's NO blank line between test cases.
[url_str] and [str] only contains lower case letters 'a' - 'z', '.', '/', ':'.
The length of [url_str] and [str] is greater than 0 and won't exceed 100.
Output
For each test case, display addresses as required, each address on a separated line. Add a blank line after each 'Display' operation.
Sample Input
1
10
Visit http://acm.whu.edu.cn
Visit http://acm.pku.edu.cn
Visit http://acm.timus.ru
Visit http://acm.whu.edu.cn
Visit http://acm.whu.edu.cn
Visit http://acm.pku.edu.cn
Display http://acm
Visit baidu.com
Visit www.whu.edu.cn
Display b
Sample Output
http://acm.whu.edu.cn
http://acm.pku.edu.cn
http://acm.timus.ru
baidu.com
Source
分析
首先,这题的背景就是搜索量的问题,如果一个地址被visit的次数越多,那么你在根据关键字搜索的时候就越容易搜到他,
然后题目还有说如果两个网址的搜索量相同,就根据字典序来输出。
很容易就想到用map来使网址和出现次数进行对应。然后自己构造一个结构体去进行排序输出;
(map的基本用法http://blog.csdn.net/it_yuan/article/details/22697205)
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
typedef struct AA
{
string s;
int c;
};
bool cmp(AA a, AA b)
{
if(b.c!=a.c)
return a.c>b.c;
else
return a.s<b.s;
}
int main()
{
int t, n, i, j;
string a, b;
scanf("%d", &t);
while(t--)
{
map<string, int> m;
map<string, int>::iterator it;
scanf("%d", &n);
while(n--)
{
cin>>a>>b;
vector<AA> v;
AA c;
if(a=="Visit")
m[b]++;
else
{
for(it=m.begin();it!=m.end();it++)
{
if((*it).first.find(b)==0)
{
c.s=(*it).first,c.c=(*it).second;
v.push_back(c);
}
}
sort(v.begin(), v.end(), cmp);
for(i=0;i<v.size();i++)
cout<<v[i].s<<endl;//这里只能用cout, 不能用printf,
puts("");
}
}
}
return 0;
}
但这个是后期从网上参考的代码,自己一开始的想法是利用set去解决,自己开一个自己构造的结构体
的数组,然后判断比较下一次输入的地址是否出现,出现了几次(一起加起来),要display的时候自己sort一下,再在结构体的s
中比较头几个字母是否和关键字一样,然后输出。只是有了这个想法,感觉比较麻烦,所以去网上看看是否有更精简的做法,
就有了上面这篇代码