POJ 3193 Cow Phrasebook(简单字符串的处理)

 

Cow Phrasebook

 

Time Limit: 1000MS

 

Memory Limit: 65536K

Total Submissions: 2358

 

Accepted: 796

 

Description

 

Ever the innovator, Farmer John is teaching cows to speak some phrases in human language. He writes each new phrase the cows learn in his phrase book, a total of M (1 <= M <= 1,000) so far. 

When Farmer John is on a vacation, he can only communicate with his cows by telephone. Being an active vacationer, FJ visits beaches and fine restaurants when away from the farm. When he returns, his answering machine is full of N (1 <= N <= 10,000) messages, many potentially from the cows.
 

FJ vacations frugally and thus gets poor telephone service. Many of the recorded messages cut off before they are complete. Help FJ determine whether the recorded messages are from the phrasebook by determining if the message begins a phrase.
 

You will be given the phrasebook and a set of texts of the recorded messages. Count the number of recorded messages that are the beginning (prefix) of a phrase from the phrase book.
 

Complete phrases are from 1 to 60 characters in length, each of which is either a letter (upper or lower case), a period (.), comma (,), question mark (?) or space. There are no leading spaces, trailing spaces, or double spaces in a phrase. Comparisons are case-sensitive, and a phrase is in fact, considered to be prefix of itself.

 

Input

 

Line 1: Two space-separated integers, M and N 

Lines 2..M+1: Phrases from the phrase book, one per line.
 

Lines M+2..M+N+1: Phrases spoken by cows, one per line.

 

Output

 

Line 1: A single integer that is the count of the number of messages that were proper prefixes of the phrasebook.

 

Sample Input

 

3 4

 

I will not buy this record, it is scratched.

 

My hovercraft is full of eels.

 

Do you want to come back to my place? Bouncy, bouncy.

 

I will not buy this rec

 

My helicopter is

 

Do you want to come back

 

I will not buy this cat.

 

Sample Output

 

2

 

Hint

 

Explanation of the sample: 

Three input phrases (record, eels, bouncy); four query phrases (buy, helicopter, come back, cat).
 

The messages 'I will not buy this rec' and 'Do you want to come back' are valid prefixes of the phrasebook.

 

Source

 

USACO 2006 February Bronze

 

 解题报告:这个题就是简单地字符串的匹配问题,暴力匹配就行;

代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAX = 1010;
char sentence[MAX][65];
int main()
{
    int N, M, i, j, ans, k;
    char str[65];
    scanf("%d%d", &M, &N);
	getchar();
    for (i = 0; i < M; ++i)
    {
        gets(sentence[i]);
    }
    int len;
	ans = 0;
    for (i = 0; i < N; ++i)
    {
        gets(str);
        len = strlen(str);
        for (j = 0; j < M; ++j)//暴力枚举
        {
			for (k = 0; k < len; ++k)
			{
				if (str[k] != sentence[j][k])
				{
					break;
				}
			}
            if (k == len)//若完全匹配
            {
				ans ++;
                break;
            }
        }
    }
    printf("%d\n", ans);
    return 0;
}

  

 

转载于:https://www.cnblogs.com/lidaojian/archive/2012/04/07/2436591.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值