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