strstr 的使用
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 的使用的更多相关文章
- [PHP源码阅读]strpos、strstr和stripos、stristr函数
我在github有对PHP源码更详细的注解.感兴趣的可以围观一下,给个star.PHP5.4源码注解.可以通过commit记录查看已添加的注解. strpos mixed strpos ( strin ...
- [LeetCode] Implement strStr() 实现strStr()函数
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- strstr 函数的实现
strstr函数:返回主串中子字符串的位置后的所有字符. #include <stdio.h> const char *my_strstr(const char *str, const c ...
- 28. Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- Leetcode 详解(Implement strstr)
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- LintCode StrStr
1. 讨论目标字符串若为空, 则返回-1: 资源字符串若为空, 则返回-1. 2.讨论目标字符串个数为零, 则返回0: 资源字符串个数为零, 则返回-1. 3. 插入旗帜来使第二循环的结束为有条件地返 ...
- strstr函数
原型:char * strstr( char *haystack, char *needle ) 用法:#include <string.h> 功能:在haystack中寻找needle ...
- strstr函数的用法
C语言函数 编辑 包含文件:string.h 函数名: strstr 函数原型: extern char *strstr(char *str1, const char *str2); 语法: ...
- [leetcode 27]Implement strStr()
1 题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...
- C语言(函数)学习之strstr strcasestr
C语言(函数)学习之[strstr]&[strcasestr]一.strstr函数使用[1]函数原型char*strstr(constchar*haystack,constchar*needl ...
随机推荐
- HDU 5738 Eureka(极角排序)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5738 [题目大意] 给出平面中一些点,在同一直线的点可以划分为一个集合,问可以组成多少包含元素不少 ...
- Google Maps API V2
1. 在AndroidManifest.xml的application节点中,添加Google play service的版本号: <meta-data android:name="c ...
- uestc 10 In Galgame We Trust
题意:求最长的合法括号序列 解:栈+分类讨论 now表示已经算出的序列,且此序列与现在扫描的序列可能能够连接,tmp表示现在扫描到的序列长度 左括号入栈 右括号:1.栈空时:统计当前总长 并且将栈,n ...
- E=MC2 - 搜搜百科
E=MC2 - 搜搜百科 1 E=MC2 质能等价理论是爱因斯坦狭义相对论的最重要的推论,即著名的方程式E=mC^2,式中E为能量,m为质量,C为光速:也就是说,一切物质都潜藏着质量乘于光速平方的能量 ...
- Java面试题之六
三十一.如何取小数点前两位,并四舍五入. package com.lovo; public class TestDot2 { // 方法1 public void test1(double c) { ...
- ReactNative遇到的一些坑
1. 如果通过一个url进行下载上传操作,这个url中包含有中文的话,一定要记得将这个url转为URLEncode编码. 如: encodeURI('http://test/有中文.doc'); 2. ...
- 对XXX(数字)安全卫士实在是忍无可忍了,为什么一定要像日本鬼子强奸妇女一样强奸我们这些弱小者
一直一来对XXX(数字)安全卫士非常痛恨,无耻,恶心,没有底线,还有对待我们这些弱小者,就像当年日本鬼子强奸妇女一样,血粼粼的虐杀我们这些弱小者,无法反抗,又必须接受. 你强制杀掉别人的ADB 就算了 ...
- 错误提示:在此上下文中不允许使用名称 "***"。有效表达式包括常量、 常量表达式和变量(在某些上下文中),不允许使用列名。
出现这种情况的原因,是因为在SQL语句的编写格式不正确. 事例展示: 错误: string sql = "insert into person ([name], sex, salary) v ...
- 浅谈SpringMVC(二)
一.SpringMVC的拦截器 1.写类implements HandlerInterceptor public class MyMvcInterceptor implements HandlerIn ...
- Chain of Responsibility模式
熟悉VC/MFC的都知道,VC是“基于消息,事件驱动”,消息在VC开发中起着举足轻重的作用.MFC提供了消息的处理的链式处理策略,处理消息的请求将沿着预定好的路径依次进行处理.消息的发送者并不知道该消 ...