UVa 10115 Automatic Editing (字符串中间替换技巧)
2013-09-29 09:24
477 查看
10115 - Automatic Editing
Time limit: 3.000 secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=96&page=show_problem&problem=1056
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 |
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 thefind 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 |
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
Example output:
behind the goat shoe or shop
注意strcpy()和strcat()的组合使用技巧。
完整代码:
/*0.015s*/ #include<cstdio> #include<cstring> char find[50][100], rep[50][100], str[300], tmp[1000]; int main(void) { int t, i; char *beg, *end; while (scanf("%d\n", &t), t) { for (i = 0; i < t; i++) { gets(find[i]); gets(rep[i]); } gets(str); for (i = 0; i < t; i++) { while (beg = strstr(str, find[i]))///赋值并判断非空 { end = beg + strlen(find[i]); strcpy(tmp, rep[i]); strcat(tmp, end); strcpy(beg, tmp); } } puts(str); } return 0; }
相关文章推荐
- UVA 10115 - Automatic Editing(字符串替换问题)
- UVA 10115-Automatic Editing(运用字符串函数对字符串进行替换)
- UVA 10115 Automatic Editing (字符串的替换和查找)
- UVa10115 - Automatic Editing- 字符串(子串替换)-难度2
- UVa10115_Automatic Editing csdn(小白书字符串专题)
- UVA 10115 子符串替换
- 正则表达式替换字符串的一个技巧 --- 可用于关键词变红色
- iOS开发技巧之:截取字符串中两个指定字符串中间的字符串
- PHP入门教程之字符串处理技巧总结(转换,过滤,解析,查找,截取,替换等)
- [php] 对数内的字符串用str_replace进行替换的技巧
- uva 10115(字符串)
- UVA 10115 (暑假-字符串(2) -F - Automatic Editing)
- UVa 10340 All in All (字符串处理&编程技巧)
- SAS命令应用高级技巧--如何从一批SCL,SOURCE文件中查找或替换某字符串
- C#将字符串中间部分替换为*号
- php替换字符串中间字符为省略号的方法
- 【C/C++】将字符串中间的空格替换为"%20"
- Automatic Editing - UVa 10115 字符串处理
- UVA 10115 - Automatic Editing(字符串)
- JS实现用特殊符号替换字符串的中间部分区域的实例代码