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 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. Continue until all the rules have been considered. Note that (1) when searching for a find string, 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-by strings 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 简单的字符串替换问题,用strstr方便许多。
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<string> using namespace std ; int main() {
int n ;
while(scanf("%d",&n) != EOF) {
char str1[50][100] , str2[150][100] , s[500] ;
if(n == 0)
return 0 ;
getchar() ;
int i ;
for(i = 0 ; i < n ; i++) {
gets(str1[i]) ;
gets(str2[i]) ;
}
gets(s) ;
for(i = 0 ; i < n ; i++) {
if(strstr(s,str1[i]) != NULL) {
char _str[500] ;
strcpy(_str,strstr(s,str1[i]) + strlen(str1[i])) ;
*(strstr(s,str1[i])) = '\0' ;
strcat(s,str2[i]) ;
strcat(s,_str) ;
i-- ;
}
}
puts(s) ;
}
return 0 ;
}

strstr 的使用的更多相关文章

  1. [PHP源码阅读]strpos、strstr和stripos、stristr函数

    我在github有对PHP源码更详细的注解.感兴趣的可以围观一下,给个star.PHP5.4源码注解.可以通过commit记录查看已添加的注解. strpos mixed strpos ( strin ...

  2. [LeetCode] Implement strStr() 实现strStr()函数

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  3. strstr 函数的实现

    strstr函数:返回主串中子字符串的位置后的所有字符. #include <stdio.h> const char *my_strstr(const char *str, const c ...

  4. 28. Implement strStr()

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  5. Leetcode 详解(Implement strstr)

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  6. LintCode StrStr

    1. 讨论目标字符串若为空, 则返回-1: 资源字符串若为空, 则返回-1. 2.讨论目标字符串个数为零, 则返回0: 资源字符串个数为零, 则返回-1. 3. 插入旗帜来使第二循环的结束为有条件地返 ...

  7. strstr函数

    原型:char * strstr( char *haystack,  char *needle ) 用法:#include <string.h> 功能:在haystack中寻找needle ...

  8. strstr函数的用法

    C语言函数 编辑 包含文件:string.h 函数名: strstr 函数原型:      extern char *strstr(char *str1, const char *str2); 语法: ...

  9. [leetcode 27]Implement strStr()

    1 题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...

  10. C语言(函数)学习之strstr strcasestr

    C语言(函数)学习之[strstr]&[strcasestr]一.strstr函数使用[1]函数原型char*strstr(constchar*haystack,constchar*needl ...

随机推荐

  1. kvm在线磁盘扩展

    1,查看指定kvm虚拟机的现有磁盘domblklist

  2. 用sqlserver处理excel表格

    本来最近在研究微信公众平台的,老大临时交我个任务,把excel表格里的数据导入sql数据库,我想这so easy嘛. 没想都在上面消磨了两天... 把情况介绍下:在数据库中有如下这样结构的表(A表) ...

  3. jeecms 2012 源码分析(2) 前台栏目页静态化分析

    还是要说到web.xml文件 <welcome-file-list> <welcome-file>index.html</welcome-file> <wel ...

  4. Python文件或目录操作的常用函数

    ◆ os.listdir(path) Return a list containing the names of the entries in the directory given by path. ...

  5. poj2311

    博弈论——sg,mex sg性质:1.在末态的状态点为N态. 2.P态的下一步有一个是N态 3.N态的下一步全部是P态. 当然这是对于单点一个游戏的情形,也相当于NIM只有一堆石子. mex(mini ...

  6. DDR、DDR2、DDR3产品区别

    DDR采用一个周期来回传递一次数据,因此传输在同时间加倍,因此就像工作在两倍的工作频率一样.为了直观,以等效的方式命名,因此命名为DDR 200 266 333 400. DDR2尽管工作频率没有变化 ...

  7. 在Linux下使用iconv转换字符串编码

    在Linux下写C程序,尤其是网络通信程序时经常遇到编码转换的问题,这里要用到iconv函数库. iconv函数库有以下三个函数 123456 #include <iconv.h>icon ...

  8. WPF:在XmlDataProvider上使用主-从绑定(Master-Detail Binding)

    原文 http://www.cnblogs.com/mgen/archive/2011/06/19/2084553.html 示例程序: 如上程序截图,一目了然典型的主从模式绑定应用,如果里面的数据不 ...

  9. 脑波设备mindwave TGCD接口开发示例

    对于TGCD的开发,神念科技提供的文件包括,头文件thinkgear.h,thinkgear.lib,thinkgear.dll,有这三个文件,在win32下开发就不是什么难事了吧 如果是java语言 ...

  10. clistctrl 虚拟列表

    一.什么是虚拟列表控件 虚拟列表控件是指带有LVS_OWNERDATA风格的列表控件.. 二.为什么使用虚拟列表控件 我们知道,通常使用列表控件CListCtrl,需要调用InsertItem把要显示 ...