8.25

· · 个人记录

字符串函数

具体用法

字符串删除函数 erase();

在字符串中查找函数 s.find();

字符串截取子串函数 substr();

错题纠正

B4048

正确思路 使用字符串删除函数+在字符串中查找函数

错误思路 使用continue+暴力

正确代码

#include<bits/stdc++.h>
using namespace std;
int main(){
    int t;
    cin>>t;
    string s,ss="not";
    cin>>s;
    int n=s.size();
    int ans=0;
    int x=0;
    while(s.find(ss)!=string::npos){
        ans++;
        int x=0;
        while(s.find(ss,x)!=string::npos){
            int y=s.find(ss,x);
            s.erase(y,3);
            x=y;
        }
    }
    cout<<s;
    cout<<endl;
    cout<<ans;
    return 0;
} 

B3769

正确思路 使用字符串截取子串函数

错误思路 使用暴力

正确代码

#include<bits/stdc++.h>
using namespace std;
int main(){
    string s,t;
    cin>>s;
    cin>>t;
    int p;
    cin>>p;
    while(p--){
        int a,b,c,d;
        cin>>a>>b>>c>>d;
        string ss,tt;
        ss=s.substr(a-1,b-a+1);
        tt=t.substr(c-1,d-c+1);
        if(ss<tt){
            cout<<"yifusuyi"<<endl;
        }
        else if(ss==tt){
            cout<<"ovo"<<endl;
        }
        else{
            cout<<"erfusuer"<<endl;
        }
    }
    return 0;
}