UVa 123 Searching Quickly (使用STL replace进行字符串替换)
2013-10-21 21:00
555 查看
123 - Searching Quickly
Time limit: 3.000 secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=98&page=show_problem&problem=59
Background
Searching and sorting are part of the theory and practice of computer science. For example, binary search provides a good example of an easy-to-understand algorithm with sub-linear complexity. Quicksort is an efficient[average
case] comparison based sort.
KWIC-indexing is an indexing method that permits efficient ``human search'' of, for example, a list of titles.
The Problem
Given a list of titles and a list of ``words to ignore'', you are to write a program that generates a KWIC (Key Word In Context) index of the titles. In a KWIC-index, a title is listed once for each keyword that occursin the title. The KWIC-index is alphabetized by keyword.
Any word that is not one of the ``words to ignore'' is a potential keyword.
For example, if words to ignore are ``
the, of, and, as, a'' and the list of titles is:
Descent of Man The Ascent of Man The Old Man and The Sea A Portrait of The Artist As a Young Man
A KWIC-index of these titles might be given by:
a portrait of the ARTIST as a young man the ASCENT of man DESCENT of man descent of MAN the ascent of MAN the old MAN and the sea a portrait of the artist as a young MAN the OLD man and the sea a PORTRAIT of the artist as a young man the old man and the SEA a portrait of the artist as a YOUNG man
The Input
The input is a sequence of lines, the string::is used to separate the list of words to ignore from the list of titles. Each of the words to ignore appears in lower-case letters on a line by itself and
is no more than 10 characters in length. Each title appears on a line by itself and may consist of mixed-case (upper and lower) letters. Words in a title are separated by whitespace. No title contains more than 15 words.
There will be no more than 50 words to ignore, no more than than 200 titles, and no more than 10,000 characters in the titles and words to ignore combined. No characters other than 'a'-'z', 'A'-'Z', and white space
will appear in the input.
The Output
The output should be a KWIC-index of the titles, with each title appearing once for each keyword in the title, and with the KWIC-index alphabetized by keyword. If a word appears more than once in a title, each instanceis a potential keyword.
The keyword should appear in all upper-case letters. All other words in a title should be in lower-case letters. Titles in the KWIC-index with the same keyword should appear in the same order as they appeared in the
input file. In the case where multiple instances of a word are keywords in the same title, the keywords should be capitalized in left-to-right order.
Case (upper or lower) is irrelevant when determining if a word is to be ignored.
The titles in the KWIC-index need NOT be justified or aligned by keyword, all titles may be listed left-justified.
Sample Input
is
the
of
and
as
a
but
::
Descent of Man The Ascent of Man The Old Man and The Sea A Portrait of The Artist As a Young Man
A Man is a Man but Bubblesort IS A DOG
Sample Output
a portrait of the ARTIST as a young man the ASCENT of man a man is a man but BUBBLESORT is a dog DESCENT of man a man is a man but bubblesort is a DOG descent of MAN the ascent of MAN the old MAN and the sea a portrait of the artist as a young MAN a MAN is a man but bubblesort is a dog a man is a MAN but bubblesort is a dog the OLD man and the sea a PORTRAIT of the artist as a young man the old man and the SEA a portrait of the artist as a YOUNG man
首先用set存ignore words,然后读入title,查找非ignore words然后将非ignore words和修改过的title放入multimap。
完整代码:
/*0.015s*/ #include<cstdio> #include<cstring> #include<string> #include<set> #include<map> using namespace std; const int maxn = 10005; char str[maxn], title[maxn], word[maxn]; string temp; set<string> ignore; multimap<string, string> ans; ///键为keyword,值为大写了keyword的title multimap<string, string>::iterator iter; int main() { int n, i, j, k, len, beg; while (gets(str), str[0] != ':') ignore.insert(str); for (n = 0; gets(title); ++n) { len = strlen(title); for (i = 0; i < len; ++i) title[i] = tolower(title[i]); for (i = 0; i < len; ++i) { if (!isalpha(title[i])) continue; beg = i; j = 0; while (i < len && isalpha(title[i])) word[j++] = title[i++]; word[j] = 0; if (!ignore.count(word)) { for (k = 0; k < j; ++k) word[k] = toupper(word[k]); temp = title; temp.replace(beg, j, word); ans.insert(make_pair(word, temp)); } } } for (iter = ans.begin(); iter != ans.end(); ++iter) puts((iter->second).c_str()); return 0; }
相关文章推荐
- 使用CASE WHEN进行字符串替换处理
- JS使用replace()方法和正则表达式进行字符串的搜索与替换实例
- JS使用replace()方法和正则表达式进行字符串的搜索与替换实例
- os x下使用sed进行字符串替换
- python使用re进行字符串查找和替换
- python使用re进行字符串查找和替换
- 一个关于使用c语言进行对文档读取替换特定字符串
- 使用命令批量对文件中出现的字符串进行替换
- JavaScript使用Replace进行字符串替换的方法
- 使用Regex.Replace()进行字符串的替换
- JS使用replace()方法和正则表达式进行字符串的搜索与替换实例
- UVA 10115-Automatic Editing(运用字符串函数对字符串进行替换)
- 【python cookbook】替换字符串中的子串(使用Template)
- 使用API函数CryptProtectData和CryptUnprotectData进行字符串加解密
- .net 使用 Aspose.Words 进行 Word替换操作
- 使用的string.format替换字符串
- .net MVC 使用 JSON JavaScriptSerializer 进行序列化或反序列化时出错,字符串的长度超过了为 maxJsonLength 属性设置的值
- 如何在PHP中使用正则表达式进行查找替换
- 在 Notepad++ 或 EditPlus 中使用正则表达式进行高级查找替换
- mysql使用REPLACE替换字段里数据内容部分字符串