UVa Automatic Editing

uva的题真的很好,每个题都能长许多知识,A了后很开心,这道题我用了两天写,只一道题就学了四个函数,成长不少

Problem E: Automatic Editing

Source file:

autoedit.{c, cpp, java, pas}

Input file:

autoedit.in

Output file:

autoedit.out

Text-processing tools like awk and sed allow you to automatically perform a sequence of editing operations based on a script. For this problem we consider the specific case in which we want to perform a series of string replacements, within a single line of text, based on a fixed set of rules. Each rule specifies the string to find, and the string to replace it with, as shown below.

Rule Find Replace-by 1. ban bab 2. baba be 3. ana any 4. ba b hind the g

To perform the edits for a given line of text, start with the first rule. Replace the first occurrence of the find string within the text by thereplace-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. Continue until all the rules have been considered. Note that (1) when searching for a findstring, you always start searching at the beginning of the text, (2) once you have finished using a rule (because the find string no longer occurs) you never use that rule again, and (3) case is significant.

For example, suppose we start with the line

banana boat

and apply these rules. The sequence of transformations is shown below, where occurrences of a find string are underlined and replacements are boldfaced. Note that rule 1 was used twice, then rule 2 was used once, then rule 3 was used zero times, and then rule 4 was used once.

Before After banana boat babana boat babana boat bababa boat bababa boat beba boat beba boat behind the goat

The input contains one or more test cases, followed by a line containing only 0 (zero) that signals the end of the file. Each test case begins with a line containing the number of rules, which will be between 1 and 10. Each rule is specified by a pair of lines, where the first line is the find string and the second line is the replace-by string. Following all the rules is a line containing the text to edit. For each test case, output a line containing the final edited text.

Both find and replace-by strings will be at most 80 characters long. Find strings will contain at least one character, but replace-bystrings may be empty (indicated in the input file by an empty line). During the edit process the text may grow as large as 255 characters, but the final output text will be less than 80 characters long.

The first test case in the sample input below corresponds to the example shown above.

Example input:

4 ban bab baba be ana any ba b hind the g banana boat 1 t sh toe or top 0

Example output:

behind the goat shoe or shop

题意:替换,在再字符窜中找到能够替换的就替换。

 Note that (1) when searching for a findstring, you always start searching at the beginning of the text, (2) once you have finished using a rule (because the find string no longer occurs) you never use that rule again, and (3) case is significant.

For example, suppose we start with the line。找到一个就重新开始,一旦结束了一个语法就不能在使用了。题意。。。。

#include<stdio.h>
#include<string.h>
struct Node
{
    int len1,len2;
    char nam1[100],nam2[100];
}word[100];

int main()
{
    int n,i,j;
    char *pt,*px;
    char str[500],rem[500],ss[500];
    int cas,flag;
    while(scanf("%d",&n) && n)
    {
        getchar();
        for (i=0;i<n;i++)
        {
            gets(word[i].nam1);
            word[i].len1=strlen(word[i].nam1);
            gets(word[i].nam2);
            word[i].len2=strlen(word[i].nam2);
        }
        gets(str);
        strcpy(rem,str);
        for (i=0;i<n;i++)//单词个数
        {
            flag=0;
            pt=strstr(rem,word[i].nam1);
            if(pt!=NULL) flag=1;
            else continue;
            cas=0;
            for (px=rem;px!=pt;px++)//存储前半部分
            {
                ss[cas++]=*px;
            }
            ss[cas]='\0';
            pt+=word[i].len1;//抛弃该换掉的地方
            strcat(ss,word[i].nam2);//接入新的
            strcat(ss,pt);//接入剩余部分
            strcpy(rem,ss);
            if(flag) i-=1;
        }
        printf("%s\n",rem);
    }
    return 0;
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏一个会写诗的程序员的博客

第12章 元编程与注解、反射第12章 元编程与注解、反射

反射(Reflection)是在运行时获取类的函数(方法)、属性、父类、接口、注解元数据、泛型信息等类的内部信息的机制。这些信息我们称之为 RTTI(Run-T...

10920
来自专栏函数式编程语言及工具

Scalaz(53)- scalaz-stream: 程序运算器-application scenario

    从上面多篇的讨论中我们了解到scalaz-stream代表一串连续无穷的数据或者程序。对这个数据流的处理过程就是一个状态机器(state machine...

20490
来自专栏海天一树

小朋友学C++(14):两数交换

之前学C语言的时候,咱们直接在main函数中使用“异或”位运算符,很容易实现了两数交换。 本节课将在此基础上,把交换两个数的算法,封装到swap函数中。这样不管...

31270
来自专栏积累沉淀

【译】Java 8的新特性—终极版

声明:本文翻译自Java 8 Features Tutorial – The ULTIMATE Guide,翻译过程中发现并发编程网已经有同学翻译过了:...

263100
来自专栏一个会写诗的程序员的博客

ES6 极简教程(ES6 Tutorial) 文 / 东海陈光剑ES6 极简教程(ES6 Tutorial)Kotlin 开发者社区

JavaScript是ECMAScript的实现和扩展,由ECMA(一个类似W3C的标准组织)参与进行标准化。ECMAScript定义了:

10730
来自专栏函数式编程语言及工具

Scalaz(47)- scalaz-stream: 深入了解-Source

   scalaz-stream库的主要设计目标是实现函数式的I/O编程(functional I/O)。这样用户就能使用功能单一的基础I/O函数组合成为功能完...

30050
来自专栏跟着阿笨一起玩NET

对Jquery+JSON+WebService的一点认识

Jquery作为一款优秀的JS框架,简单易用的特性就不必说了。在实际的开发过程中,使用JQ的AJAX函数调用WebService

22620
来自专栏javathings

Spring 中如何控制对象的初始化时间(延迟加载,强制先行加载)

当标注了@Lazy 注解时候,不会看到 init user… 的输出。只有当首次使用 User 类的时候,才会被初始化。

69120
来自专栏分享达人秀

Activity间数据传递方法汇总

在Activity间传递的数据一般比较简单,但是有时候实际开发中也会传一些比较复杂的数据,本节一起来学习更多Activity间数据的传递。 一、常用数据...

39060
来自专栏偏前端工程师的驿站

编译期类型检查 in ClojureScript

前言  话说"动态类型一时爽,代码重构火葬场",虽然有很多不同的意见(请参考),但我们看到势头强劲的TypeScript和Flow.js,也能感知到静态类型在某...

20070

扫码关注云+社区

领取腾讯云代金券