题目1168:字符串的查找删除(字符串操作)

题目链接:http://ac.jobdu.com/problem.php?pid=1168

详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus

参考代码:

//
//  1168 字符串的查找删除2.cpp
//  Jobdu
//
//  Created by PengFei_Zheng on 25/04/2017.
//  Copyright © 2017 PengFei_Zheng. All rights reserved.
//
 
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <cmath>
#define MAX_SIZE 1001
 
using namespace std;
 
char str[MAX_SIZE];
 
int main(){
    gets(str);
    string a = str;
    int lena = (int)a.size();
    for(int i = 0 ; i < lena ; i++){
        a[i] = tolower(a[i]);
    }
    while(gets(str)){
        string b = str;
        string c = b;
        int lenb = (int)b.size();
        for(int i = 0 ; i < lenb ; i++){
            b[i]=tolower(b[i]);
        }
        int pos = (int)b.find(a,0);
        while(pos!=string::npos){
            c.erase(pos,lena);
            b.erase(pos,lena);
            pos = (int)b.find(a,pos);
        }
        pos = (int)c.find(' ',0);
        while(pos!=string::npos){
            c.erase(pos,1);
            pos = (int)c.find(' ',0);
        }
        cout<<c<<endl;
    }
    return 0;
}
/**************************************************************
    Problem: 1168
    User: zpfbuaa
    Language: C++
    Result: Accepted
    Time:0 ms
    Memory:1520 kb
****************************************************************/

参考代码2:

//
//  1168 字符串的查找删除.cpp
//  oj
//
//  Created by PengFei_Zheng on 03/04/2017.
//  Copyright © 2017 PengFei_Zheng. All rights reserved.
//
 
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
 
 
char c[1001];
int match_len = 0;
char ch;
 
using namespace std;
 
int main(){
     
    scanf("%s",c);
    int len = (int)strlen(c);//the length of pattern
    ch=getchar();//remove the space or return after input c
    while((ch=getchar())!=EOF){//get each char
        if(tolower(ch)==tolower(c[match_len])) {//find same character
            match_len++;//move index to next position
            if(match_len>=len) match_len=0;// reinitation ,if ch==c[len-1] then next i will be assigned len
        }
        else{
            if(match_len==0){//now position is not satisfy
                if(ch!=' ') cout<<ch;//cout now char
            }
            else{//the array c is just right save the cahracter which is not cout to the screen
                for(int k=0;k<match_len;k++){//now i keeps to the position max_match_length
                    cout<<(c[k]);
                }
                match_len=0;//reinitation i to 0
                if(ch != ' ')  putchar(ch);//cout right now character
            }
        }
    }
     
}
/**************************************************************
    Problem: 1168
    User: zpfbuaa
    Language: C++
    Result: Accepted
    Time:0 ms
    Memory:1520 kb
****************************************************************/
原文地址:https://www.cnblogs.com/zpfbuaa/p/6767977.html