UVA 123 Searching Quickly
2015-08-21 15:12
435 查看
B - Searching Quickly
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld
& %llu
Submit Status
Description
efficient
[average case] comparison based sort.
KWIC-indexing is an indexing method that permits efficient ``human search'' of, for example, a list of titles.
that occurs in 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 ``
A KWIC-index of these titles might be given by:
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.
instance is 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.
答案:
题解
STL之set集合容器
multimap 多重映照容器
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld
& %llu
Submit Status
Description
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 anefficient
[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 keywordthat occurs in 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, eachinstance is 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
答案:
#include <iostream> #include <stdio.h> #include <cstring> #include <string> #include <cctype> #include <map> #include <set> using namespace std; int main () { set<string> ignore;//创建set集合对象 multimap<string, string> r; string t; int i; //采用inset()方法把元素插入到集合中 while (getline(cin, t) && t != "::") ignore.insert(t); while (getline(cin, t)) { for ( i = 0; i < t.size(); i++) t[i] = tolower(t[i]); for (int i = 0; i < t.size(); i++) { if (!isalpha(t[i])) continue; string t2; int j = i; while (isalpha(t[j])) { t2 += t[j]; j++; } //判断字符串是否在集合中 if (!ignore.count(t2)) { for (int k = 0; k < t2.size(); k++) { t2[k] = toupper(t2[k]); } string t3 = t; t3.replace(i, t2.size(), t2); r.insert(make_pair(t2, t3)); } i = j; } } for (multimap<string, string> :: iterator i = r.begin(); i != r.end(); i++) cout << i->second << endl; return 0; }题解:
题解
STL之set集合容器
multimap 多重映照容器
相关文章推荐
- 从UIImage的矩阵变换看矩阵运算的原理
- iOS摄像头和相册-UIImagePickerController-浅析
- iOS UIView设置背景图片
- Android stduio NDK开发
- 快速理解RequireJs
- UIView实现动画效果
- AsyncTask介绍
- UIView之userInteractionEnabled属性介绍
- hdu5412CRB and Queries(整体二分)
- WPF:如何在工作线程中更新窗体的UI元素(Dispatcher机制)
- UIView 的Transform。(旋转,缩放,3D旋转)
- UIScrollView
- 自定义UIPageControl 的Circle 与其他实现方式有几行代码改动,亲自试过
- iOS 3D UI---CALayer的transform扩展
- [LeetCode] Unique Paths
- forward与include转发问题(RequestDispatcher.include()(乱码问题分析及解决)
- Mix and Build(简单DP)
- Mix and Build(简单DP)
- UITextField详解
- 解决query不到本地视频方法