题目链接

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:

 /*************************************************************************
> File Name: 113B.cpp
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年07月18日 星期五 22时08分53秒
> Propose:
************************************************************************/
#include <set>
#include <cmath>
#include <string>
#include <cstdio>
#include <vector>
#include <fstream>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; string t, b, e;
int flag1[], flag2[];
int len1, len2, len3;
typedef unsigned long long ull;
vector<ull> ans; void
solve(int x) {
ull tt = ;
for (int i = x; i < len1; i++) {
tt = tt * + t[i];
if (flag2[i] == && i-x+ >= max(len2, len3)) ans.push_back(tt);
}
} int
main(void) {
cin >> t;
cin >> b;
cin >> e;
len1 = (int)t.size();
len2 = (int)b.size();
len3 = (int)e.size();
int maxl = max(len2, len3);
for (int i = ; i < len1; i++) {
if (t.substr(i, len2) == b) flag1[i] = ;
if (t.substr(i, len3) == e) flag2[i+len3-] = ;
}
for (int i = ; i < len1; i++) if (flag1[i]) {
solve(i);
}
sort(ans.begin(), ans.end());
ans.resize(unique(ans.begin(), ans.end())-ans.begin());
printf("%d\n", ans.size()); return ;
}

Codeforces 113B的更多相关文章

  1. CodeForces 113B Petr#

    题目链接:http://codeforces.com/problemset/problem/113/B 题目大意: 多组数据每组给定3个字符串T,Sbeg,Sed,求字符串T中有多少子串是以Sbeg开 ...

  2. 【Codeforces 113B】Petr#

    Codeforces 113 B 题意:有一个母串\(S\)以及两个串\(S_{begin}\)和\(S_{end}\),问\(S\)中以\(S_{begin}\)为开头并且以\(S_{end}\)为 ...

  3. CodeForces - 113B Petr# (后缀数组)

    应该算是远古时期的一道题了吧,不过感觉挺经典的. 题意是给出三一个字符串s,a,b,求以a开头b结尾的本质不同的字符串数. 由于n不算大,用hash就可以搞,不过这道题是存在复杂度$O(nlogn)$ ...

  4. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  5. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  6. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  7. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  8. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  9. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

随机推荐

  1. scrapy中下载文件和图片

    下载文件是一种很常见的需求,例如当你在使用爬虫爬取网站中的图片.视频.word.pdf.压缩包等的时候 scrapy中提供了FilesPipeline和ImagesPipeline,专门用来下载文件和 ...

  2. Apache Pig入门学习文档(一)

    1,Pig的安装    (一)软件要求    (二)下载Pig      (三)编译Pig 2,运行Pig    (一)Pig的所有执行模式    (二)pig的交互式模式    (三)使用pig脚本 ...

  3. [转]浅谈C#中常见的委托

    一提到委托,浮现在我们脑海中的大概是听的最多的就是类似C++的函数指针吧,呵呵,至少我的第一个反应是这样的. 关于委托的定义和使用,已经有诸多的人讲解过,并且讲解细致入微,尤其是张子阳的那一篇.我就不 ...

  4. iTerm2配色和去掉profile提示框

    效果: 配色方案代码地址: https://github.com/mbadolato/iTerm2-Color-Schemes 点击最右边的绿色区域,再点击  “import”, 打开刚下载解压好的文 ...

  5. 关于dictionary和tuple充当函数参数

    需要接收dict时,使用 **name: 需要接收tuple时,使用 *name: --> *name参数后面的任何数据会被认为是’keyword-only’,即它们只能被当作关键词而非参数使用 ...

  6. 阿里云提供全托管 ZooKeeper

    自 2010 年左右第一次引入以来,Apache ZooKeeper 目前在阿里巴巴集团内部已经有了将近 10 年的发展,使用的场景非常广泛,基于 ZooKeeper 强一致性的特点,被用在了分布式锁 ...

  7. C语言函数指针和回调函数

    彻底搞定C指针-函数名与函数指针 函数名&函数名取地址 函数指针 通常我们可以将指针指向某类型的变量,称为类型指针(如,整型指针).若将一个指针指向函数,则称为函数指针. 函数名的意义 函数名 ...

  8. 错误 2 error C2059: 语法错误:“::”

    设置项目属性,在预定义处理器中添加定义NOMINMAX来禁止使用Vsual C++的min/max宏定义. 项目属性   ——> C/C++ ——> 预处理器 ——> 预处理器定义 ...

  9. Django中间件初始化过程

    def load_middleware(self): """ Populate middleware lists from settings.MIDDLEWARE. Mu ...

  10. linux平台进行c语言源码安装

    安装c源程序的步骤: 1. ./configure --prefix 执行编译检测 指定安装路径 2. make 编译 3. sudo make install 编译后安装 前两步可以合成一步(mak ...