POJ 2803 字符串识别

本程序通过识别前缀和后缀来扩展词汇定义,能够处理多种变化形式,显著减少根词定义的数量。有效处理了包括'un','pre','re'等前缀及'ing','ize','tion'等后缀。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Defining Moment
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1341 Accepted: 603

Description

As a homework assignment, you have been tasked with creating a program that provides the meanings for many different words. As you dislike the idea of writing a program that just prints definitions of words, you decide to write a program that can print definitions of many variations of just a handful of different root words. You do this by recognizing common prefixes and suffixes. Since your program is smart enough to recognize up to one prefix and one suffix per word, it can process many forms of each word, significantly reducing the number of rote definitions required.

For this problem, you'll be writing the prefix/suffix processing portion of the program.

Valid prefixes and their meanings are:

anti<word> against <word>
post<word> after <word>
pre<word> before <word>
re<word> <word> again
un<word> not <word>


Valid suffixes and their meanings are:

<word>er one who <word>s
<word>ing to actively <word>
<word>ize change into <word>
<word>s multiple instances of <word>
<word>tion the process of <word>ing


Note that suffixes are tied more tightly to their root word and should therefore be expanded last. For example, the word ``vaporize" would be expanded through the following steps:
 	unvaporize

not vaporize
not change into vapor

Of course, the definitions are not exactly right, but how much polish does the professor expect for a single homework grade?


Input

Input to this problem will begin with a line containing a single integer n indicating the number of words to define. Each of the following n lines will contain a single word. You need to expand at most one prefix and one suffix, and each word is guaranteed to have a non-empty root (i.e., if the prefix and/or suffix are removed, a non-empty string will remain). Each word will be composed of no more than 100 printable characters.

Output

For each word in the input, output the expanded form of the word by replacing the prefix and/or suffix with its meaning.

Sample Input

6
vaporize
prewar
recooking
root
repopularize
uninforming

Sample Output

change into vapor
before war
to actively cook again
root
change into popular again
not to actively inform

Source

要非常注意细节的处理,本人就是由于具体的细节没有处理好,导致错了很多次都不知道在哪儿错了。。

Source Code

Problem: 2803 User: bingshen
Memory: 172K Time: 0MS
Language: C++ Result: Accepted

  • Source Code
    #include<stdio.h>
    #include<string.h>
    
    char word[105];
    
    int main()
    {
    	int n,l,i;
    //	freopen("in.txt","r",stdin);
    //	freopen("out.txt","w",stdout);
    	scanf("%d",&n);
    	while(n--)
    	{
    		memset(word,'/0',sizeof(word));
    		bool flag=false;
    		bool loop=false;
    		scanf("%s",word);
    		l=strlen(word);
    		if(word[0]=='u'&&word[1]=='n')
    		{
    			for(i=0;i<l-2;i++)
    				word[i]=word[i+2];
    			word[l-2]='/0';
    			printf("not ");
    		}
    		if(word[0]=='p'&&word[1]=='r'&&word[2]=='e')
    		{
    			for(i=0;i<l-3;i++)
    				word[i]=word[i+3];
    			word[l-3]='/0';
    			printf("before ");
    		}
    		if(word[0]=='a'&&word[1]=='n'&&word[2]=='t'&&word[3]=='i')
    		{
    			for(i=0;i<l-4;i++)
    				word[i]=word[i+4];
    			word[l-4]='/0';
    			printf("against ");
    		}
    		if(word[0]=='p'&&word[1]=='o'&&word[2]=='s'&&word[3]=='t')
    		{
    			for(i=0;i<l-4;i++)
    				word[i]=word[i+4];
    			word[l-4]='/0';
    			printf("after ");
    		}
    		if(word[0]=='r'&&word[1]=='e')
    		{
    			for(i=0;i<l-2;i++)
    				word[i]=word[i+2];
    			word[l-2]='/0';
    			flag=true;
    		}
    		l=strlen(word);
    		if(word[l-3]=='i'&&word[l-2]=='n'&&word[l-1]=='g')
    		{
    			word[l-3]='/0';
    			printf("to actively %s",word);
    			if(!flag)
    				printf("/n");
    			loop=true;
    		}
    		if(word[l-3]=='i'&&word[l-2]=='z'&&word[l-1]=='e')
    		{
    			word[l-3]='/0';
    			printf("change into %s",word);
    			if(!flag)
    				printf("/n");
    			loop=true;
    		}
    		if(word[l-4]=='t'&&word[l-3]=='i'&&word[l-2]=='o'&&word[l-1]=='n')
    		{
    			word[l-4]='/0';
    			printf("the process of %s",word);
    			printf("ing");
    			if(!flag)
    				printf("/n");
    			loop=true;
    		}
    		if(word[l-1]=='s')
    		{
    			word[l-1]='/0';
    			printf("multiple instances of %s",word);
    			if(!flag)
    				printf("/n");
    			loop=true;
    		}
    		if(word[l-2]=='e'&&word[l-1]=='r')
    		{
    			word[l-2]='/0';
    			printf("one who %s",word);
    			printf("s");
    			if(!flag)
    				printf("/n");
    			loop=true;
    		}
    		if(flag&&!loop)
    			printf("%s again/n",word);
    		if(flag&&loop)
    			printf(" again/n");
    		if(!flag&&!loop)
    			printf("%s/n",word);
    	}
    	return 0;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值