(以“分类:字符串处理 ==摘要== {{信息题|Wallace and His Pet|http://acm.whu.edu.cn/land/problem/detail?problem_id{{=}}1522|1|100|没考虑大写情况|0|201...”为内容创建页面) |
小 (修正标签) |
||
| 第1行: | 第1行: | ||
[[分类:字符串处理]] | [[分类:字符串处理]] | ||
==摘要== | ==摘要== | ||
| − | {{信息题|Wallace and His Pet|http://acm.whu.edu.cn/land/problem/detail?problem_id{{=}}1522|1|100|没考虑大写情况|0|2014-10-30 19:44:28}} | + | {{信息题|Wallace and His Pet|http://acm.whu.edu.cn/land/problem/detail?problem_id{{=}}1522|1|100|没考虑大写情况|0|time=2014-10-30 19:44:28}} |
==题意== | ==题意== | ||
把每个句子中出现次数最多的单词替换成guagua。 | 把每个句子中出现次数最多的单词替换成guagua。 | ||
| 题目链接 | 难度等级 | 完成状态 | 完成分数 | 最后编辑时间 | 失误原因(初次提交分数) |
|---|---|---|---|---|---|
| Wallace and His Pet | ★☆☆☆☆ | 答案正确 | 100 | 2014-10-30 19:44:28 | 没考虑大写情况(0) |
把每个句子中出现次数最多的单词替换成guagua。
map+string搞定(string库用法见CodeVS/1204,STL用法详见CodeVS/1075参考链接)。
第一次没考虑大小写的问题,做法是先整句处理成小写,然后搜索匹配小写后的句子,替换原句相应处的字符,这样大小写不会乱。
大小写转换用的是algorithm和cctype库里的一个函数,用法是:
transform(被转换字符串.begin(),被转换字符串.end(),存储位置.begin(),towlower或towupper); //例如: transform(str.begin(),str.end(),str.begin(),towlower);
至于读取一行后再分割的技巧,在CodeVS/1205谈过了,就不再细说。
| 1522.cpp代码已折叠
展开折叠内容
|
|---|
#include<sstream>
#include<iostream>
#include<cstdio>
#include<string>
#include<map>
#include<algorithm>
#include<cctype>
using namespace std;
int main()
{
int n;
scanf("%d\n",&n);
while(n--)
{
map<string,int> u;
string s,maxps;
getline(cin,s,'\n');
string ls=s;
transform(ls.begin(),ls.end(),ls.begin(),towlower);
stringstream sio;
sio<<ls;
string p;
int last=0,maxp=0;
while(sio>>p)
{
if(p[p.size()-1]=='.')
p.erase(p.size()-1,1);
++u[p];
if(u[p]>maxp)
{
maxp=u[p];
maxps=p;
}
}
while(ls.find(maxps,last)!=string::npos)
{
last=ls.find(maxps,last);
ls.replace(last,maxps.size(),"guagua");
s.replace(last,maxps.size(),"guagua");
++last;
}
cout<<s<<endl;
}
return 0;
}
|