uva 409(字符串模拟)

不算难的题,就是有个小陷阱。
先是告诉你几个关键词 (keywords)
再告诉你几个“借口”(excuse)
让你输出存在keyworld最多的excuse.
excuse可以存在多组、
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
    int judge(char excuse[],char keywords[]);
    int n,m;
    int max ;
    char keywords[50][50];
    int cnt[50];
    char excuse[50][100];
    int kkk = 0;
    while(cin>>n>>m)
    {
        max = -1;
        memset(cnt,0,sizeof(cnt));
        memset(excuse,0,sizeof(excuse));
        memset(keywords,0,sizeof(keywords));
       for(int i = 0 ; i < n ; i++)
       {
           cin>>keywords[i];
       }
       getchar();
       for(int i = 0 ; i < m ; i++)

       {
           gets(excuse[i]);
       }
       for(int i = 0 ; i < m ; i++)
       {
           for(int j = 0 ; j < n ; j++)
           {
               if(judge(excuse[i],keywords[j]))
               {
                   cnt[i]++;
               }
           }
       }
       for(int i = 0 ; i < m ; i++)/*输出存在特定子串最多的字符串*/
       {
           if(cnt[i] > max)
           {
               max = cnt[i];
           }
       }
       cout<<"Excuse Set #"<<++kkk<<endl;
       for(int i = 0 ; i < m ; i++)
       {
           if(cnt[i] == max)
           {
               cout<<excuse[i]<<endl;
           }
       }
       cout<<endl;
    }
    return 0;
}
int judge(char excuse[],char keywords[]) /*朴素匹配在字符串中查找某一子串是否存在*/
{
    int j = 0;
    for(int i = 0 ; excuse[i] != '\0' ; )
    {
        if(keywords[j] == excuse[i] || keywords[j] == excuse[i] - 'A' + 'a')
        {
            j++;
            i++;
            if(j == strlen(keywords))
            {
                if((excuse[i] >= 'a' && excuse[i] <= 'z') || (excuse[i] <= 'Z' && excuse[i] >= 'A')) 
                {
                    return 0;/*判断一个keywords在excuse中存在时其后一个字符是否为字母*/
                            /*跟我们平时阅读习惯相同,单词与单词间必须有间隔,*/
                }
                return 1;
            }
        }
        else
        {
            j = 0;
            i = i - j + 1;
        }
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值