牛客网---删除公共字符串
发布日期:2021-05-10 10:38:51 浏览次数:17 分类:精选文章

本文共 2147 字,大约阅读时间需要 7 分钟。

������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

������������������������������size_t find���������������������������������������������������������������������������������������������������������������������������������������������������������

���������������

  • ���������������2���������������������������������������������������������������������������1������
  • ������������������������������find���������������������������������������������������������������������������������������������������������������������
  • ������������������������������������������������������������������erase���������������������������������������������������
  • ���������������������������������������������������������

    #include 
    #include
    #include
    using namespace std;
    int main() {
    string str1, str2;
    getline(cin, str1);
    getline(cin, str2);
    size_t pos = 0;
    while (pos <= str2.size()) {
    size_t found = str1.find(str2[pos], pos);
    if (found == string::npos) {
    pos++;
    continue;
    }
    if (found > pos) {
    str1.erasefound(pos, found - pos);
    }
    pos = found + 1;
    }
    cout << "���������������������1���" << str1 << endl;
    return 0;
    }

    ���������������

  • ���������������������getline������������������������������str1���str2���
  • ������������������������pos������������������������������������
  • ������������������������������pos������������������2������������
    • ���������������������str1.find(str2[pos], pos)���������������������������str2[pos]���
    • ���������������������������������������������������found���������������������������������������
  • ���������������������str1.erase(found, found - pos)���������������������found������������������������������found - pos������������������������������
  • ���������������������pos���found + 1���������������������������������
  • ������������������������������������������1���
  • ������������������������������������2������������������������������������1���������������

    上一篇:牛客网---数组中出现次数超过一半的数字
    下一篇:Linux---线程池

    发表评论

    最新留言

    表示我来过!
    [***.240.166.169]2025年04月06日 13时00分28秒