题解 | #单词替换#
单词替换
https://www.nowcoder.com/practice/5b58a04679d5419caf62c2b238e5c9c7
#include <iostream>
#include<bits/stdc++.h>
#include <sstream>
using namespace std;
int main() {
vector<string > st ;
string tmp ;
while(getline(cin , tmp))
{
istringstream is(tmp) ;
while(!is.eof())
{
string t;
is>>t ;
st.push_back(t) ;
}
string willbereplace ;
getline(cin , willbereplace) ;
string toreplace ;
getline(cin , toreplace) ;
for(int i = 0 ; i < st.size() ; ++ i)
{
if(st[i] == willbereplace)
{
st[i] = toreplace ;
}
}
for(int i = 0 ; i < st.size() ; ++ i)
{
cout<<st[i]<<" ";
}
}
// st.push_
}
// 64 位输出请用 printf("%lld")
使用了额外的空间。所以空间效率不高
