文章

5

粉丝

0

获赞

25

访问

1.1k

头像
字符串匹配 - 兰州大学 题解:c++解法
P1717 兰州大学机试题
发布于2026年1月10日 20:40
阅读数 291

#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    string a,b;
    cin>>a>>b;
   int pos=b.find(a);   //用pos接收模式串a在文本串b中的位置,找不到则返回-1
   
   if(pos==string::npos)        //如果pos是无效的索引,说明模式串不是文本串的子字符串,则输出-1
   {
    cout<<"-1"<<endl;   
   }
    while(pos!=string::npos) //若模式串是文本串的子字符串
    {
        b.erase(pos,a.size());  //用string类自带的erase函数删除文本串中的模式串
        pos=b.find(a);          //继续找文本串中还有没有其它模式串,更新pos
    }
    for(int i=0;i<b.size();i++)     //输出结果
        {
            cout<<b[i];
        }
 
 
  
    
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发