UVa_123 - 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 tex2html_wrap_inline29 [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


题意:

定义一种新的检索方法,给定若干无关的词语(ignore),对应给定的title除了ignore词,其余都是关键词,按照关键词顺序排序,然后按照要求输出。

解决方案:

题很简单,需要注意一些细节。用c写比较繁琐,各种函数都要自己写,用C++写估计不超过50行代码。

struct keys
{
    char str[30];
    int flag;
    int left;
    int right;
}keyword[MAXSIZE];

定义该结构体:

str数组用于保存关键词,flag用于标记是哪个title;

left和right用于记录关键词在原title的位置(因为输出要求关键词大写,且一句title中可能有相同的关键词,需要保存下位置)


代码如下:

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#define MAXSIZE 10010

char ignore[51][12];
char title[205][155];
struct keys
{
    char str[30];
    int flag;
    int left;
    int right;
}keyword[MAXSIZE];

int cmp(const void* _a, const void* _b)
{
    struct keys* a=(struct keys *)_a;
    struct keys* b=(struct keys *)_b;
    if(strcmp(a->str,b->str)!=0) return strcmp(a->str,b->str);
    else return a->flag-b->flag;
}

bool Search(char *a,int n)
{
    for(int i=0;i<n;i++){
        if(strcmp(a,ignore[i])==0) return false; //判断是ignore词
    }
    return true; //判断是keyword
}

int main()
{
    int n,m,k;//n,m分别为ignore和title的个数,k为关键词的个数
    int i,j;
    char buf[20];
    //freopen("123.txt","r",stdin);
    //freopen("1243_out.txt","w",stdout);
    n=0;
    while(scanf("%s",buf)&&buf[0]!=':')
    {
        strcpy(ignore[n++],buf);
    }
    getchar();
    m=0;k=0;
    while(gets(title[m])!=NULL)
    {
        int len=strlen(title[m]);
        for(i=0;i<len;i++){
           if(isalpha(title[m][i])) title[m][i]=tolower(title[m][i]);
        }
        j=0;
        for(i=0;i<len;i++){
           if(isalpha(title[m][i])) {
               if(j==0) keyword[k].left=i;
               buf[j++]=title[m][i];
               if(i==len-1) {
                   buf[j]='\0';
                   keyword[k].right=i;
                   if(Search(buf,n)) { strcpy(keyword[k].str,buf);keyword[k].flag=m;k++; }
                }
           }
           else{
               buf[j]='\0';
               keyword[k].right=i-1;
               if(Search(buf,n)) { strcpy(keyword[k].str,buf);keyword[k].flag=m; k++;}
               j=0;
           }
        }
        m++;
    }
    qsort(keyword,k,sizeof(keyword[0]),cmp);
    for(i=0;i<k;i++){

        for(j=0;j<strlen(title[keyword[i].flag]);j++){
            if(j>=keyword[i].left&&j<=keyword[i].right) putchar(toupper(title[keyword[i].flag][j]));
            else putchar(title[keyword[i].flag][j]);
        }
        putchar('\n');
    }
	return 0;
}







Delphi 12.3 作为一款面向 Windows 平台的集成开发环境,由 Embarcadero Technologies 负责其持续演进。该环境以 Object Pascal 语言为核心,并依托 Visual Component Library(VCL)框架,广泛应用于各类桌面软件、数据库系统及企业级解决方案的开发。在此生态中,Excel4Delphi 作为一个重要的社区开源项目,致力于搭建 Delphi 与 Microsoft Excel 之间的高效桥梁,使开发者能够在自研程序中直接调用 Excel 的文档处理、工作表管理、单元格操作及宏执行等功能。 该项目以库文件与组件包的形式提供,开发者将其集成至 Delphi 工程后,即可通过封装良好的接口实现对 Excel 的编程控制。具体功能涵盖创建与编辑工作簿、格式化单元格、批量导入导出数据,乃至执行内置公式与宏指令等高级操作。这一机制显著降低了在财务分析、报表自动生成、数据整理等场景中实现 Excel 功能集成的技术门槛,使开发者无需深入掌握 COM 编程或 Excel 底层 API 即可完成复杂任务。 使用 Excel4Delphi 需具备基础的 Delphi 编程知识,并对 Excel 对象模型有一定理解。实践中需注意不同 Excel 版本间的兼容性,并严格遵循项目文档进行环境配置与依赖部署。此外,操作过程中应遵循文件访问的最佳实践,例如确保目标文件未被独占锁定,并实施完整的异常处理机制,以防数据损毁或程序意外中断。 该项目的持续维护依赖于 Delphi 开发者社区的集体贡献,通过定期更新以适配新版开发环境与 Office 套件,并修复已发现的问题。对于需要深度融合 Excel 功能的 Delphi 应用而言,Excel4Delphi 提供了经过充分测试的可靠代码基础,使开发团队能更专注于业务逻辑与用户体验的优化,从而提升整体开发效率与软件质量。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
-- Performing Test HAVE_GCC_THREAD_LOCAL_STORAGE -- Performing Test HAVE_GCC_THREAD_LOCAL_STORAGE - Success -- Performing Test HAVE_MSC_THREAD_LOCAL_STORAGE -- Performing Test HAVE_MSC_THREAD_LOCAL_STORAGE - Failed -- Performing Test HAVE_CONSTRUCTOR_ATTRIBUTE -- Performing Test HAVE_CONSTRUCTOR_ATTRIBUTE - Failed -- Performing Test HAVE_DESTRUCTOR_ATTRIBUTE -- Performing Test HAVE_DESTRUCTOR_ATTRIBUTE - Failed -- Performing Test HAVE_FALLTHROUGH_ATTRIBUTE -- Performing Test HAVE_FALLTHROUGH_ATTRIBUTE - Failed -- Performing Test HAVE_GCC_VOLATILE_MEMORY_PROTECTION -- Performing Test HAVE_GCC_VOLATILE_MEMORY_PROTECTION - Failed -- Performing Test HAVE_GCC_NARG_MACRO -- Performing Test HAVE_GCC_NARG_MACRO - Failed -- Performing Test HAVE_COMPILER__FUNC__ -- Performing Test HAVE_COMPILER__FUNC__ - Failed -- Performing Test HAVE_COMPILER__FUNCTION__ -- Performing Test HAVE_COMPILER__FUNCTION__ - Failed -- Performing Test HAVE_GCC_BOUNDED_ATTRIBUTE -- Performing Test HAVE_GCC_BOUNDED_ATTRIBUTE - Failed -- Performing Test HAVE_LD_VERSION_SCRIPT -- Performing Test HAVE_LD_VERSION_SCRIPT - Failed -- Check if the system is big endian -- Searching 16 bit integer -- Looking for sys/types.h -- Looking for sys/types.h - not found -- Looking for stddef.h -- Looking for stddef.h - not found -- Check size of unsigned short -- Check size of unsigned short - failed -- Check size of unsigned int -- Check size of unsigned int - failed -- Check size of unsigned long -- Check size of unsigned long - failed CMake Error at /usr/share/cmake-3.5/Modules/TestBigEndian.cmake:51 (message): no suitable type found Call Stack (most recent call first): ConfigureChecks.cmake:417 (test_big_endian) CMakeLists.txt:98 (include) -- Configuring incomplete, errors occurred! See also "/home/bba/work/BBA1_5/MBB_Platform/apps/public/libssh/build/CMakeFiles/CMakeOutput.log". See also "/home/bba/work/BBA1_5/MBB_Platform/apps/public/libssh/build/CMakeFiles/CMakeError.log". make/apps/pubapps/libssh.mk:9: recipe for target 'libssh' failed make: *** [libssh] Error 1
10-08
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值