【hihoCoder】1082: 然而沼跃鱼早就看穿了一切

  题目:http://hihocoder.com/problemset/problem/1082

输入一个字符串,将其中特定的单词替换成另一个单词

  代码注意点:

1. getline(istream &in, string &s)

  • 没有读入字符,将返回false

2. transform(tmp.begin(),tmp.end(),tmp.begin(),::tolower)

  • 将tmp的所有字母都改成小写
  • 需要包含 #include <algorithm>

3. 查找函数:size_t find(const string& str, size_t pos = 0) const

  • 用法:string s, t; s.find(t, 0); 从s的0位置开始查找t是不是出现在s中
  • 返回出现t在s中出现的位置。如果没有找到,则返回 string::npos

4. 替换函数:string& replace(size_type pos(开始替换的位置),size_type cnt(替换的个数), const basic_string str(用来替换的字符串))

  • 用法:string s, t; s.replace(0, 3, t); 从pos开始删除cnt个字母,然后在pos位置插入str
  • 返回替换后的字符串

  源码  

 #include <iostream>
#include<string>
#include <vector>
#include <algorithm>
using namespace std; int main()
{
string str;
string temp;;
string target="marshtomp";
string Des="fjxmlhx";
string ::size_type tLength=target.length();
string ::size_type dLength=Des.length();
vector<string> output;
//getline(cin,str);
while (getline(cin,str) )
{
temp=str;
//转换成小写
transform(temp.begin(),temp.end(),temp.begin(),::tolower);
string ::size_type pos=;
//可能有多处匹配成功的位置!!!
while ( (pos=temp.find(target,pos))<str.length())
{
str.replace(pos,tLength,Des);
temp.replace(pos,tLength,Des);
pos+=dLength;
}
output.push_back(str);
}
for (vector<string>::size_type i=;i<output.size();i++)
cout<<output[i]<<endl;
return ;
}
上一篇:【原创】洛谷 LUOGU P3379 【模板】最近公共祖先(LCA) -> 倍增


下一篇:微信小程序之性能优化