POJ 1572 Automatic Editing 字符串替换,replace就够了

  题意不难理解,但是一开始还是没有看清楚题目。Replace the first occurrence of the find string within the text by the replace-by string, then try to perform the same replacement again on the new text. Continue until the find string no longer occurs within the text, and then move on to the next rule.这个地方我贡献了几次TL。它的意思是替换完第n个字符串后,继续判断是否还能被这个字符串替换,否则移至下一个。

#include <iostream>
#include <cstdio>
#include <string>
using namespace std;

int main()
{
    //freopen("in.txt","r",stdin);
    while(1)
    {
        int n;
        scanf("%d",&n);
        if(n==0)
            break;
        string str1[12];
        string str2[12];
        getchar();
        for(int i=0; i<n; i++)
        {
            getline(cin,str1[i]);
            getline(cin,str2[i]);
        }
        string str3;
        getline(cin,str3);
        for(int i=0; i<n; i++)
            while(1)
            {
                int index=str3.find(str1[i]); //查找要替换的字符串位置,没有返回string::npos
                if(index==string::npos)
                    break;
                str3=str3.replace(index,str1[i].length(),str2[i]); //replace函数直接替换,或者用erase和insert也行
                //str3.erase(index,str1[i].length());
                //str3.insert(index,str2[i]);
            }
        cout<<str3<<endl;
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/pach/p/5720430.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值