O.Small Tiger’s Love Letter(统计单词出现次数)

 ou_fan De代码

Description

Small Tiger loves a girl very much and one day he wants to send a love letter to her.In this letter, there are at most 1000000 latin characters without space.

After he send it to his dreamlover, she asks him a hard question:I give you a string, if you can tell me how many times does this string appear in your letter in 1 second, then I will be your girlfriend!

Small Tiger is worried about this, and can you help him to solve this question?

Input

There are multi tests. The first line of input file contains a single number t, means there are t cases. Each test case has the following format:

One line with the string A which the girl tells small tiger.

One line with the love letter B.

t < 100, 0 < |A|, |B| < 1000000.

Output

For every test case in the input file, the output should begin with a string “Case #n: ”, which n means the number of the test case. And then output a integer which means the answer.

Sample Input

1

love

Iloveyouverymuchlove

Sample Output

Case #1: 2

Hint

This is not a real story because Small Tiger is singer now!

You can learn KMP algorithm to AC this problem!

 

#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int main() {
    int t;
    cin >> t;
    getchar();
    int num = 1;
    while (t--) {
        string s1, s2;
        int ans = 0;
        getline(cin, s2);
        getline(cin, s1);
        transform(s1.begin(), s1.end(), s1.begin(), ::tolower);
        transform(s2.begin(), s2.end(), s2.begin(), ::tolower);
        int pos = s1.find(s2);//开始查找,有的话则不为-1;
        while (pos != -1) {
                ans++;
                pos=s1.find(s2, pos+1);
        }
        printf("Case #%d: %d\n",num,ans);
            num++;
    }
}

使用到了find函数和简单的stl

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ou_fan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值