Codeforces 113B

题目链接

B. Petr#
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Long ago, when Petya was a schoolboy, he was very much interested in the Petr# language grammar. During one lesson Petya got interested in the following question: how many different continuous substrings starting with the sbegin and ending with the send (it is possible sbegin = send), the given string t has. Substrings are different if and only if their contents aren't equal, their positions of occurence don't matter. Petya wasn't quite good at math, that's why he couldn't count this number. Help him!

Input

The input file consists of three lines. The first line contains string t. The second and the third lines contain the sbegin and sendidentificators, correspondingly. All three lines are non-empty strings consisting of lowercase Latin letters. The length of each string doesn't exceed 2000 characters.

Output

Output the only number — the amount of different substrings of t that start with sbegin and end with send.

Sample test(s)
input
round
ro
ou
output
1
input
codeforces
code
forca
output
0
input
abababab
a
b
output
4
input
aba
ab
ba
output
1
Note

In the third sample there are four appropriate different substrings. They are: ab, abab, ababab, abababab.

In the fourth sample identificators intersect.

思路:首先在字符串t中找到sbegin和send的位置, 也就是下面代码中两个数组flag1和flag2的作用。

对于每个sbegin的出现位置,向后扫一遍t,因为sbegin和send可以重合,所以要求被加入ans 中的字符串长度要不小于sbegin和send的最小长度。

只需要加入字符串的hash值, 最后去重即可。

这里,用到的两个stl函数, 一个是substr(begin, length), 第一个参数为字符串的起始地址,第二个参数为要去字串的长度。

第二个函数是unique函数, unique(begin, end, compare), 这里第三个参数compare是比较函数,一般的类型比较都可以省略,

unique用来将相邻的重复元素移至数组的末尾,而不是直接删除,其返回值为指向第一个出现重复的元素的迭代器,并且一般和sort配合使用。

 Accepted Code:

 1 /*************************************************************************  2  > File Name: 113B.cpp  3  > Author: Stomach_ache  4  > Mail: [email protected]  5  > Created Time: 2014年07月18日 星期五 22时08分53秒  6  > Propose:  7  ************************************************************************/

 8 #include <set>

 9 #include <cmath>

10 #include <string>

11 #include <cstdio>

12 #include <vector>

13 #include <fstream>

14 #include <cstring>

15 #include <iostream>

16 #include <algorithm>

17 using namespace std; 18 

19 string t, b, e; 20 int flag1[2002], flag2[2002]; 21 int len1, len2, len3; 22 typedef unsigned long long ull; 23 vector<ull> ans; 24 

25 void

26 solve(int x) { 27     ull tt = 0; 28       for (int i = x; i < len1; i++) { 29           tt = tt * 31 + t[i]; 30         if (flag2[i] == 1 && i-x+1 >= max(len2, len3)) ans.push_back(tt); 31  } 32 } 33 

34 int

35 main(void) { 36         cin >> t; 37           cin >> b; 38         cin >> e; 39         len1 = (int)t.size(); 40         len2 = (int)b.size(); 41         len3 = (int)e.size(); 42         int maxl = max(len2, len3); 43         for (int i = 0; i < len1; i++) { 44               if (t.substr(i, len2) == b) flag1[i] = 1; 45             if (t.substr(i, len3) == e) flag2[i+len3-1] = 1; 46  } 47         for (int i = 0; i < len1; i++) if (flag1[i]) { 48  solve(i); 49  } 50  sort(ans.begin(), ans.end()); 51         ans.resize(unique(ans.begin(), ans.end())-ans.begin()); 52         printf("%d\n", ans.size()); 53 

54     return 0; 55 }

 

你可能感兴趣的:(codeforces)