Searching Quickly 

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 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, each 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.

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

题目大意:给出一些ingore word(可忽略单词),再给出一些句子,句子由可忽略单词和不可忽略单词组成, 要求找出所有的不可忽略单词,输出含不可忽略单词的句子(此时不可忽略单词要大写),注意一个不可忽略单词可能出现在多个句子里,一个句子可能有多个(包括相同的)不可忽略单词(输出时按照不可忽略单词的字典序,相同单词按照句子的顺序)

解题思路:读入不可忽略单词,再读入句子,每读入一个句子时将句子分解成单词,分解的同时判断它是否为不可忽略单词(与前面可忽略单词进行比较),读完所有的句子以后对不可忽略单词进行排序,然后再对每个不可忽略单词查找每条语句。

PS:题目本身没有难度,但是要细心,因为比较繁琐。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; #define N 10005
#define T 205
#define M 20 struct say{
char word[M];
}s[T]; struct talk{
char sen[T][M];
int cnt;
}title[T]; int n_s = 0, n_title = 0, n_ignore = 0;;
char ignore_word[T][M]; void change(char str[]){
int len = strlen(str);
for (int i = 0; i < len; i++){
if(str[i] >= 'A' && str[i] <= 'Z')
str[i] += 32;
}
} int cmp(const say a, const say b){
return strcmp(a.word, b.word) < 0;
} void print(talk t, int k){
for (int i = 0; i < t.cnt - 1; i++){
if (i == k){
for (int j = 0; j < strlen(t.sen[i]); j++)
printf("%c", t.sen[i][j] - 32);
printf(" ");
}
else
printf("%s ", t.sen[i]);
}
if (t.cnt - 1 == k){
for (int j = 0; j < strlen(t.sen[t.cnt - 1]); j++)
printf("%c", t.sen[t.cnt - 1][j] -32);
printf("\n");
}
else
printf("%s\n", t.sen[t.cnt - 1]);
} void find(talk t, say a){
for (int i = 0; i < t.cnt; i++){
if (strcmp(t.sen[i], a.word) == 0)
print(t, i);
}
} void judge(char str[]){
for (int i = 0; i < n_ignore; i++){
if (strcmp(ignore_word[i], str) == 0)
return;
}
for (int i = 0; i < n_s; i++){
if (strcmp(s[i].word, str) == 0)
return;
}
strcpy(s[n_s++].word, str);
} void build(char str[], int k){
int len = strlen(str), n = 0, m = 0;
for (int i = 0; i < len; i++){
if (str[i] >= 'a' && str[i] <= 'z')
title[k].sen[n][m++] = str[i];
else{
title[k].sen[n][m] = '\0';
judge(title[k].sen[n]);
m = 0;
n++;
}
}
title[k].sen[n++][m] = '\0';
judge(title[k].sen[n - 1]);
title[k].cnt = n;
} int main(){
char name[N]; // Read.
while (1){
gets(ignore_word[n_ignore]);
if (strcmp(ignore_word[n_ignore], "::") == 0)
break;
change(ignore_word[n_ignore]);
n_ignore++;
}
while (gets(name) != NULL){
change(name);
build(name, n_title);
n_title++;
} // Ready.
sort(s, s + n_s, cmp); // Find.
for (int i = 0; i < n_s; i++)
for (int j = 0; j < n_title; j++)
find(title[j], s[i]);
return 0;}

uva 123 Searching Quickly的更多相关文章

  1. STL --- UVA 123 Searching Quickly

    UVA - 123 Searching Quickly Problem's Link:   http://acm.hust.edu.cn/vjudge/problem/viewProblem.acti ...

  2. UVa 12505 Searching in sqrt(n)

    传送门 一开始在vjudge上看到这题时,标的来源是CSU 1120,第八届湖南省赛D题“平方根大搜索”.今天交题时CSU突然跪了,后来查了一下看哪家OJ还挂了这道题,竟然发现这题是出自UVA的,而且 ...

  3. uva 1597 Searching the Web

    The word "search engine" may not be strange to you. Generally speaking, a search engine se ...

  4. 湖南省第八届大学生程序设计大赛原题 D - 平方根大搜索 UVA 12505 - Searching in sqrt(n)

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=30746#problem/D D - 平方根大搜索 UVA12505 - Searchin ...

  5. UVa123 - Searching Quickly

    题目地址:点击打开链接 C++代码: #include <iostream> #include <set> #include <map> #include < ...

  6. Volume 1. Sorting/Searching(uva)

    340 - Master-Mind Hints /*读了老半天才把题读懂,读懂了题输出格式没注意,结果re了两次. 题意:先给一串数字S,然后每次给出对应相同数目的的一串数字Si,然后优先统计Si和S ...

  7. 刘汝佳 算法竞赛-入门经典 第二部分 算法篇 第五章 3(Sorting/Searching)

    第一题:340 - Master-Mind Hints UVA:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Item ...

  8. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  9. UVA题解二

    UVA题解二 UVA 110 题目描述:输出一个Pascal程序,该程序能读入不多于\(8\)个数,并输出从小到大排好序后的数.注意:该程序只能用读入语句,输出语句,if语句. solution 模仿 ...

随机推荐

  1. poj1201/zoj1508/hdu1384 Intervals(差分约束)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Intervals Time Limit: 10 Seconds      Mem ...

  2. self parent $this关键字分析--PHP

    self :    调用本类的静态方法和属性,常量 parent :调用父类的静态方法.属性.普通方法.构造函数,不能调用父类的普通属性 $this :    调用本类的普通方法和属性,如果本类没有就 ...

  3. HTTP协议入门知识

    HTTP超文本传输协议,是客户端浏览器和服务器通信的规范,是浏览器与服务器通信的协议,属于应用层的协议,web开发者了解HTTP协议非常重要.浏览器通过http协议请求服务器,完成请求服务器立刻关闭连 ...

  4. mac terminal的使用技巧

    1. 多tab支持    1)terminal y也是支持多tab的, Cmd+T可以打开一个新的tab    2) cmd + shift + { / } 可以在tab间切换   2. termia ...

  5. SQL Server 无法打开物理文件的 2 种解决办法

    解决方法: 方法1.无法打开可以能是没有权限.如果是这样以管理员身份运行Managerment Studio就可以了. 方法2.找到指定的数据库文件.右键属性-->安全-->勾上  ‘完全 ...

  6. 运行时数据区即内存分配管理——JVM之六

    内存分配结构,请参考: http://iamzhongyong.iteye.com/blog/1333100

  7. SQL语句优化汇总(上) 感动啊 学习 收藏了

    原文地址:http://topic.csdn.net/u/20080716/11/2317d040-48e7-42da-822e-040b4c55b46d.html MS   SQL   Server ...

  8. UESTC_秋实大哥带我飞 2015 UESTC Training for Graph Theory<Problem B>

    B - 秋实大哥带我飞 Time Limit: 300/100MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit ...

  9. Word Search 解答

    Question Given a 2D board and a word, find if the word exists in the grid. The word can be construct ...

  10. 剑指offer-面试题6.重建二叉树

    题目:输入某二叉树的前序遍历和中序遍历结果,请重建出该二叉树.假设 输入的前序遍历和中序遍历的结果都不含重复的数字.例如输入前序遍历 序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2, ...