题中给的五个测试点全过,提交有一个超时一个答案错误,不知道该怎么搞了。。。(主体思路就是字符串匹配,至于一些细节坑点是真的无语,就这样吧)
#include<bits/stdc++.h>
using namespace std;
const int N = 1e2+3;
vector<string> v(N);
//set<string> se(N);
string str;
int main(){
int n,m,count = 0;
cin>>n;
for(int i = 0;i<n;i++){
string s;
cin>>s;
//v.push_back(s);//注意其推入位置不确定不可和下标索引混用(好几次离谱错误之源)
v[i] = s;
//se.intsert(s);
}
cin>>m;
// gets(str);//由于安全性问题,该函数在C++中已弃用
// puts(str);
getchar();//注意这里其不加吸收换行其就默认开始即结束
getline(cin,str);
//string stl 用法考察
// for(int i = 0;i<str.size();i++){
// for(int j = 0;j<n;j++){
// if(i+v[j].size()>=str.size()){
// continue;
// }else{
// if(str.substr(i,v[j].size())==v[j]){
// count++;
// i += v[j].size();
// }
// }
// }
// }
// for(int i = 0;i<n;i++){
// cout<<v[i]<<endl;
// }
for(int i = 0;i<n;i++){
for(int j = 0;j<str.size();j++){
//if(str.find(v[i])==-1) break;
if(j+v[i].size()>str.size()){
break;
}else{
if(str.substr(j,v[i].size())==v[i]){
count++;
//cout<<str.substr(j,v[i].size())<<" "<<v[i]<<endl;
str.replace(j,v[i].size(),"<censored>");//str动态变化
j += 10-1;
//j += v[i].size()-1;
}
}
}
}
if(count>=m){
cout<<count<<endl<<"He Xie Ni Quan Jia!";
}
else{
cout<<str;
}
//cout<<str;
return 0;
}
vector容器中下标使用
vector<int> v;
v[0]=1;//错误,因为v不包含任何元素
vector<int> v(10);
v[0]=1;//正确,此时v是含有10个0的容器
istream& getline (char* s, streamsize n );
istream& getline (char* s, streamsize n, char delim );
#include <iostream>
using namespace std;
int main()
{
char name[256];
cout << "Please input your name: ";
cin.getline(name, 256);
cout << "The result is: " << name << endl;
return 0;
}
#include <iostream>
using namespace std;
int main( )
{
char line[100];
cout << " Type a line terminated by 't'" << endl;
cin.getline( line, 100, 't' );
cout << line << endl;
return 0;
}
istream& getline (istream& is, string& str, char delim);
istream& getline (istream&& is, string& str, char delim);
istream& getline (istream& is, string& str);
istream& getline (istream&& is, string& str);
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Please input your name: ";
getline(cin, name);
cout << "Welcome to here!" << name << endl;
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Please input your name: ";
getline(std::cin, name, '#');
cout << "Welcome to here!" << name << endl;
return 0;
}
因篇幅问题不能全部显示,请点此查看更多更全内容