UVA 10115 Automatic Editing

本文介绍了一个字符串替换算法的具体实现过程,该算法在一个长字符串中查找并替换指定的子串,直到无法再找到该子串为止。文章通过C++代码详细展示了如何进行字符串匹配和替换操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在一个串中寻找某个串,找到就替换,直至再也找不到这个串为止。题目说,用完的串就不再用了,简化了许多。

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<string>
#include<cmath>
#include<map>
///LOOP
#define REP(i, n) for(int i = 0; i < n; i++)
#define FF(i, a, b) for(int i = a; i < b; i++)
#define FFF(i, a, b) for(int i = a; i <= b; i++)
#define FD(i, a, b) for(int i = a - 1; i >= b; i--)
#define FDD(i, a, b) for(int i = a; i >= b; i--)
///INPUT
#define RI(n) scanf("%d", &n)
#define RII(n, m) scanf("%d%d", &n, &m)
#define RIII(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define RIV(n, m, k, p) scanf("%d%d%d%d", &n, &m, &k, &p)
#define RV(n, m, k, p, q) scanf("%d%d%d%d%d", &n, &m, &k, &p, &q)
#define RFI(n) scanf("%lf", &n)
#define RFII(n, m) scanf("%lf%lf", &n, &m)
#define RFIII(n, m, k) scanf("%lf%lf%lf", &n, &m, &k)
#define RFIV(n, m, k, p) scanf("%lf%lf%lf%lf", &n, &m, &k, &p)
#define RS(s) scanf("%s", s)
///OUTPUT
#define PN printf("\n")
#define PI(n) printf("%d\n", n)
#define PIS(n) printf("%d ", n)
#define PS(s) printf("%s\n", s)
#define PSS(s) printf("%s ", n)
#define PC(cas) printf("Excuse Set #%d\n", cas)
///OTHER
#define PB(x) push_back(x)
#define CLR(a, b) memset(a, b, sizeof(a))
#define CPY(a, b) memcpy(a, b, sizeof(b))
#define display(A, n, m) {REP(i, n){REP(j, m)PIS(A[i][j]);PN;}}

using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int MOD = 1000000;
const int INFI = 1e9 * 2;
const LL LINFI = 1e17;
const double eps = 1e-6;
const double pi = acos(-1.0);
const int N = 333;
const int M = 22;
const int move[8][2] = {0, 1, 0, -1, 1, 0, -1, 0, 1, 1, 1, -1, -1, 1, -1, -1};

char a[M][N], b[M][N], c[N], p[N];
int x, y;

bool check(int k)
{
    int l = strlen(c), j = 0, q;
    REP(i, l)if(c[i] == a[k][j])
    {
        q = x = i;
        while(j < strlen(a[k]) && q < l && c[q] == a[k][j])q++, j++;
        if(j == strlen(a[k]))
        {
            y = q;
            return 1;
        }
        j = 0;
    }
    return 0;
}

int main()
{
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    int n, l, k;
    while(RI(n), n)
    {
        getchar();
        REP(i, n)gets(a[i]), gets(b[i]);
        gets(c);
        REP(i, n)
        {
            while(check(i))
            {
                l = 0;
                REP(j, x)p[l++] = c[j];
                REP(j, strlen(b[i]))p[l++] = b[i][j];
                FF(j, y, strlen(c))p[l++] = c[j];
                p[l++] = '\0';
                REP(j, l)c[j] = p[j];
            }
        }
        puts(c);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值