fjnu 1193 Duplicate Words

本文介绍了一个程序设计任务:读取多行文本并找出所有重复出现的单词,忽略大小写差异。文章提供了完整的源代码实现,包括字符串处理、排序和比较等功能。

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

Description

What background? Seemed like a good idea at the time...

Input

Anyway, you're to write a program that reads in a whole bunch of words (each word is no more than 20 characters) and spit out the duplicate words. For our purposes, words are to be separated by spaces, commas (,), !, ?, and periods (.). No word goes past the end of a line. There can be an arbitrary amount of duplicate words. All comparisons should ignore case.

Output

Output the duplicate words, one per line, in lower case and in sorted order.

Sample Input

How much wood would a woodchuck chuck if a woodchuck could chuck wood? Well? what do you think? Are you feeling well?

Sample Output

a
chuck
well
wood
woodchuck
you

 KEY:这题好恶心,居然一个CASE搞了好多行……搞死了……丢人

 

Source:

#include
<iostream>
#include
<string>
#include
<map>
#include
<algorithm>
using namespace std;

char str[200][1000];

struct node
{
    
char str[25];
    
int num;
}
;

node a[
1001];
int m;
map
<string,int> mp;
int K;

int find(char str[])
{
    
for(int i=1;i<=m;i++)
        
if(!strcmp(str,a[i].str)) return i;
}


void GetList()
{
    
    
char* step=" (),.!?";
    
char* t;
    
int i;
    
for(int k=1;k<=K;k++)
    
{
        
for(i=0;i<strlen(str[k]);i++)
            
if(str[k][i]>='A'&&str[k][i]<='Z') str[k][i]=str[k][i]+32;
            t
=strtok(str[k],step);
            
while(t!=NULL)
            
{
                
if(mp[string(t)]==0)
                
{
                    strcpy(a[
++m].str,t);
                    a[m].num
++;
                    mp[
string(t)]++;
                }

                
else
                
{
                    
int pos=find(t);
                    a[pos].num
++;
                }

                t
=strtok(NULL,step);
            }

    }

}


int compare(node x,node y)
{
    
if(x.num==1&&y.num>1return 0;
    
if(x.num>1&&y.num==1return 1;
    
if(x.num==1&&y.num==1return 0;
    
if(x.num>1&&y.num>1)
    
{
        
if(strcmp(x.str,y.str)>0return 0;
        
else return 1;
    }

}


void output()
{
    
for(int i=1;i<=m;i++)
    
{
        
if(a[i].num>1)
            cout
<<a[i].str<<endl;
        
else break;
    }

}


void input()
{
    
while(gets(str[++K])) ;
}


int main()
{
//    freopen("fjnu_1193.in","r",stdin);    
    input();
    GetList();
    sort(a
+1,a+m+1,compare);
    output();
    
return 0;
}











 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值