CF 226 DIV2 B. Bear and Strings
2014-01-26 10:35
465 查看
B. Bear and Strings
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
The bear has a string s = s1s2... s|s| (record |s| is
the string's length), consisting of lowercase English letters. The bear wants to count the number of such pairs of indices i, j (1 ≤ i ≤ j ≤ |s|),
that string x(i, j) = sisi + 1... sj contains
at least one string "bear" as a substring.
String x(i, j) contains string
"bear", if there is such index k (i ≤ k ≤ j - 3),
that sk = b, sk + 1 = e, sk + 2 = a, sk + 3 = r.
Help the bear cope with the given problem.
Input
The first line contains a non-empty string s (1 ≤ |s| ≤ 5000).
It is guaranteed that the string only consists of lowercase English letters.
Output
Print a single number — the answer to the problem.
Sample test(s)
input
output
input
output
Note
In the first sample, the following pairs (i, j) match: (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9).
In the second sample, the following pairs (i, j) match: (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (1, 10), (1, 11), (2, 10), (2, 11), (3, 10), (3, 11), (4, 10), (4, 11), (5, 10), (5, 11), (6, 10), (6, 11), (7, 10), (7, 11).
这题我一看最先想到的就是容斥,可惜自己对容斥不太熟悉,最后进入的死胡同
自己用容斥会错的原因:
A∪B∪C = A+B+C - A∩B - B∩C - C∩A +A∩B∩C
我在做的是 C∩A 明显是没有减的
错误代码:
其实这题根本不要用到容斥,只要找出其中的规律即可
AC代码:
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
The bear has a string s = s1s2... s|s| (record |s| is
the string's length), consisting of lowercase English letters. The bear wants to count the number of such pairs of indices i, j (1 ≤ i ≤ j ≤ |s|),
that string x(i, j) = sisi + 1... sj contains
at least one string "bear" as a substring.
String x(i, j) contains string
"bear", if there is such index k (i ≤ k ≤ j - 3),
that sk = b, sk + 1 = e, sk + 2 = a, sk + 3 = r.
Help the bear cope with the given problem.
Input
The first line contains a non-empty string s (1 ≤ |s| ≤ 5000).
It is guaranteed that the string only consists of lowercase English letters.
Output
Print a single number — the answer to the problem.
Sample test(s)
input
bearbtear
output
6
input
bearaabearc
output
20
Note
In the first sample, the following pairs (i, j) match: (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9).
In the second sample, the following pairs (i, j) match: (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (1, 10), (1, 11), (2, 10), (2, 11), (3, 10), (3, 11), (4, 10), (4, 11), (5, 10), (5, 11), (6, 10), (6, 11), (7, 10), (7, 11).
这题我一看最先想到的就是容斥,可惜自己对容斥不太熟悉,最后进入的死胡同
自己用容斥会错的原因:
A∪B∪C = A+B+C - A∩B - B∩C - C∩A +A∩B∩C
我在做的是 C∩A 明显是没有减的
错误代码:
#include<stdio.h> #include<math.h> #include<string.h> #include<algorithm> #include<limits.h> using namespace std; int main() { char a[5555]; int t[5555];//记录bear的位置 while(scanf("%s",&a)!=EOF) { int sum= 0; int l= strlen(a); for(int i= 0; i< l-3; i++) if(a[i]=='b'&&a[i+1]=='e'&&a[i+2]=='a'&&a[i+3]=='r') { sum++; t[sum]= i; } for(int i= 1; i<= sum; i++) printf("%d ",t[i]); printf("\n"); __int64 ans= 0; for(int i= 1; i<= sum; i++) { for(int j= 1; j+i-1<= sum; j++) { __int64 be= t[j]+1; __int64 end= t[j+i-1] + 3; // if(i==2) // printf("%d %d\n",be,end); if(i%2) { ans+= be*(l- end); //printf("%d %d %d\n",i,j,be*(l-end)); printf("%d %d %d %d\n",i,be,l-end,ans); } else { ans-= be*(l- end); printf("%d %d %d %d\n",i,be,l-end,ans); } } } printf("%I64d\n",ans); } return 0; }
其实这题根本不要用到容斥,只要找出其中的规律即可
AC代码:
#include<stdio.h> #include<math.h> #include<string.h> #include<algorithm> #include<limits.h> using namespace std; int main() { char ch[5555]; while(scanf("%s",&ch)!=EOF) { int l= strlen(ch); int temp= 0; __int64 ans= 0; for(int i= 0; i< l-3; i++) if(ch[i]=='b'&&ch[i+1]=='e'&&ch[i+2]=='a'&&ch[i+3]=='r') { ans+= (i+ 1- temp) * (l- i -3); temp= i + 1; } printf("%I64d\n",ans); } return 0; }
相关文章推荐
- Codeforces Round #226 (Div. 2) B. Bear and Strings
- Codeforces Round #195 (Div. 2) D题Vasily the Bear and Beautiful Strings
- Codeforces Round #226 (Div. 2)C. Bear and Prime Numbers
- Codeforces Round #226 (Div. 2) C. Bear and Prime Numbers(素数筛法)
- Codeforces Round #226 (Div. 2)B. Bear and Strings
- [Codeforces Round #195 DIV2D (CF336D)] Vasily the Bear and Beautiful Strings
- CF(#293 div2):A:Vitaly and Strings
- Codeforces Round #226 (Div. 2) D. Bear and Floodlight
- Codeforces Round #226 (Div. 2):Problem 385C - Bear and Prime Numbers (素数刷法+前缀和)
- CF#195(div2) A:Vasily the Bear and Triangle
- Codeforces Round #226 (Div. 2)B. Bear and Strings
- Codeforces Round #293 (Div. 2) A. Vitaly and Strings(思路)
- CF 653E(Bear and Forgotten Tree 2-链表优化搜索)
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) A. Bear and Three Balls 水题
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) E. Bear and Forgotten Tree 2 bfs set 反图的生成树
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) C. Bear and Different Names 贪心
- Codeforces Round #356 (Div. 2) B. Bear and Finding Criminal 水题
- Codeforces Round #356 (Div. 2)-C. Bear and Prime 100
- Codeforces Round #358 (Div. 2) D. Alyona and Strings dp
- 【VK Cup 2016 - Round 1 (Div 2 Edition)A】【水题 暴力】Bear and Reverse Radewoosh 做题由易到难分高有难到易分高