hoj Excuses, Excuses! 字符串模拟

136 篇文章 0 订阅
25 篇文章 0 订阅
/*一个基础的字符串模拟。

先将句子用get整串读入。然后用sscanf分离出逐个单词进行匹配。

  char *p=temp;
            while(sscanf(p,"%s",tempkey)>0)
            {
                p++;
                p+=strlen(tempkey);


char *s  则s+a为从第a个字符到s的末尾。

两个过程都用temp和tempkey来实现。

预处理的时候用

  if(isalpha(temp[j]))
                    temp[j]=tolower(temp[j]);


将大写转化成小写。

非字母的部分预处理成空格。

*/

#include <stdio.h>

#include <cstring>
#include <iostream>
#include <ctype.h>
using namespace std;
int main()
{
    int n,m;
    char a[21][21];
    char b[21][71];
    char tempkey[100];
    char temp[100];
    int maxlen[100];

    int k=0;
    while(scanf("%d%d",&n,&m)==2)
    {
        int max=0;
        for(int i=1; i<101; i++)
            maxlen[i]=0;
        for(int i=1; i<=n; i++)
            scanf("%s",a[i]);
        getchar();
        for(int i=1; i<=m; i++)
        {
            gets(b[i]);
            strcpy(temp,b[i]);
            for(int j=0; temp[j]!='\0'; j++)
            {
                if(isalpha(temp[j]))
                    temp[j]=tolower(temp[j]);
                else
                    temp[j]=' ';
            }
            char *p=temp;
            while(sscanf(p,"%s",tempkey)>0)
            {
                p++;
                p+=strlen(tempkey);
                for(int j=1; j<=n; j++)
                {
                    if(strcmp(a[j],tempkey)==0)                    
                        maxlen[i]++;                      
                }
            }
            if(maxlen[i]>max)
                max=maxlen[i];
        }
        printf("Excuse Set #%d\n",++k);
        for(int i=1; i<=m; i++)
        {
            if(maxlen[i]==max)
                printf("%s\n",b[i]);
        }
        printf("\n");
    }
    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值