1491.子串删除


时间限制: 1000 MS          内存限制: 65536 K
        
提交数: 400 (0 users)          通过数: 211 (203 users)


问题描述
   给定两个字符串s和t,若s是t的子串,将t中的子串s删除,若存在多个子串,则全部删除;若s不是t的子串,对字符串t不做处理。字符串s和t长度不超过1000。


输入格式
第一行,字符串t,文本长度<=1000。
第二行,字符串s,文本长度<=1000。


输出格式
处理后的字符串t


样例输入
Hello World!
  Hello
Hello World!
  Ho
No pain, no gain
  ain


样例输出
   World! 
Hello World!
No p, no g


来源

xmu

#include <stdio.h>
#include <string.h>

void delete_substring(char *t, char *s, char *output)
{
    int len_t = (int)strlen(t);
    int len_s = (int)strlen(s);
    int ptr_t, ptr_s, ptr_output, ptr_temp;

    ptr_t = 0;
    ptr_output = 0;
    while (ptr_t < len_t)
    {
        ptr_temp = ptr_t;
        ptr_s = 0;
        while (ptr_temp < len_t && ptr_s < len_s)
        {
            if (t[ptr_temp] != s[ptr_s])
                break;
            ptr_temp++;
            ptr_s++;
        }
        if (ptr_s == len_s)
            ptr_t += len_s;
        else
            output[ptr_output++] = t[ptr_t++];
    }
    output[ptr_output] = '\0';
}

int main()
{
    char t[1005] = { 0 };
    char s[1005] = { 0 };
    char output[1005] = { 0 };

    while (gets(t))
    {
        gets(s);
        delete_substring(t, s, output);
        printf("%s\n", output);
    }

    return 0;
}