Hrbust 1389 JiaozhuV5 Substrings【思维+大模拟+细心+细心】窝为什么要做这种题T T
来源:互联网 发布:nginx server log配置 编辑:程序博客网 时间:2024/05/16 07:54
This is the definition of substring:
String x is a substring of string w if it has a non-zero length and can be found from some position in string w.(e.g. There are 6 substring of string "aba": "a", "b", "a", "ab", "ba", "aba".) Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider the number of times it occurs.
You are given a string s. Your task is to find the number of its substrings which containing exactly k "JiaozhuV5"(Case-sensitive).
InputThere are multiple test cases. The first line of input is an integer T indicating the number of test cases. Then T test cases follow.
For each test case:
Line 1. This line contains the single integer k (0 ≤ k ≤ 106).
Line 2. This line contains a non-empty string s. The length of s does not exceed 106 characters. It consists only of letters, numbers "0" to "9" and character "!".
OutputFor each test case:
Line 1. Output the number of substrings of the given string, containing exactly k "JiaozhuV5" (Case-sensitive).
Sample Input3
1
JiaozhuV5!!
2
abJiaozhuV5!!JiaozhuV5cd
100
abJiaozhuV5!!JiaozhuV5cd
Sample Output3
9
0
HintIn the first sample the sought substrings are:
JiaozhuV5
JiaozhuV5!
JiaozhuV5!!
In the second sample the sought substrings are:
JiaozhuV5!!JiaozhuV5
bJiaozhuV5!!JiaozhuV5
JiaozhuV5!!JiaozhuV5c
bJiaozhuV5!!JiaozhuV5c
abJiaozhuV5!!JiaozhuV5
JiaozhuV5!!JiaozhuV5cd
bJiaozhuV5!!JiaozhuV5cd
abJiaozhuV5!!JiaozhuV5c
abJiaozhuV5!!JiaozhuV5cd
Source哈理工2012春季校赛 - 现场赛Author齐达拉图@HRBUST题目大意:
给你一个长度不超过10^6的一个字符串,让你找一共有多少个子串,其中包含K个JiaozhuV5
思路(Wa到死真开心):
1、O(n)处理出每一个JiaozhuV5的区间左端点和右端点。
2、分类讨论:
①当k==0的时候,如果我们一个JiaozhuV5都没有,ans=len*(len+1)/2;
②当k==0的时候,如果我们有JiaozhuV5,那么我们就要把带有JiaozhuV5的每一个子序列都去掉。
③当k>0的时候,我们对于每一段都进行计算。
3、思路真的很好建立,代码真的很难写.......(调了四个小时吧.....)窝为什么要做这种题!!!!!!!!!!!!!!
Ac代码:
#include<stdio.h>#include<string.h>using namespace std;#define ll long long intchar a[1000010];ll ans[1000010];ll b[1000100];ll s[1000001];ll e[1000001];ll cal(ll num){ return num*(num+1)/2;}ll judge(ll i){ if(a[i]=='J')return 1; if(a[i]=='i')return 2; if(a[i]=='a')return 3; if(a[i]=='o')return 4; if(a[i]=='z')return 5; if(a[i]=='h')return 6; if(a[i]=='u')return 7; if(a[i]=='V')return 8; if(a[i]=='5')return 9; return 0;}int main(){ ll t; scanf("%lld",&t); while(t--) { ll n; scanf("%lld",&n); scanf("%s",a); ll cont=0; ll output=0; ll len=strlen(a); for(ll i=0; i<len; i++) { if(a[i]=='J') { b[i]=1; continue; } if(i==0) { if(a[i]=='J')b[i]=1; else b[i]=0; } else { ll tmp=judge(i); if(tmp==b[i-1]+1) { b[i]=tmp; } else b[i]=0; } } for(ll i=0; i<len; i++) { if(b[i]==9) { s[cont]=(i+1-8); e[cont]=i; cont++; } } if(n==0&&cont==0) { output+=cal(len); } else if(n==0) { output+=cal(e[0])-cal(7)+cal(len-s[cont-1]); for(ll i=0; i<cont-1; i++) { output+=cal(e[i+1]-s[i])-cal(7); } } else { if(cont==n) output+=s[0]*(len-e[n-1]); else { for(int i=0;i<=cont-n;i++) { if(i==0) { output+=s[i]*(e[i+n]-e[i+n-1]); } else if(i==cont-n) { output+=(s[i]-s[i-1])*(len-e[cont-1]); } else { output+=(s[i]-s[i-1])*(e[i+n]-e[i+n-1]); } } } } printf("%lld\n",output); }}/*11abJiaozhuV5!!JiaozhuV5cdJiaozhuV5ef*/
- Hrbust 1389 JiaozhuV5 Substrings【思维+大模拟+细心+细心】窝为什么要做这种题T T
- HDU 1238 Substrings 细心的模拟
- POJ 3923 - 模拟,要细心
- 细心
- 要细心
- hdoj5641模拟。。。真的很要细心
- 播放器(模拟题,细心)
- poj1051 细心模拟
- 细心细心再细心!!
- 我要做一个细心的人
- CF 1A1B 两个水题(简单思维+细心)
- 做一个细心的人
- 写程序要细心
- 要细心、多学多看
- codeforces 399A. Pages(模拟题,细心点就能AC)
- 做程序员要细心,并且要怀疑自己
- c++,要细心不能想当然
- C++要细心不能想当然
- 一个简单的HTML表单及JavaScript验证
- ubuntu安装samba服务
- Java 编程中关于异常处理的 10 个最佳实践
- mongodb学习笔记
- jQuery学习笔记(五) jQuery UI按钮,进度条以及滑动条
- Hrbust 1389 JiaozhuV5 Substrings【思维+大模拟+细心+细心】窝为什么要做这种题T T
- 测试我的第一个博客
- adb 命令查看 包名 对应包名 apk
- 底部提示框
- Java基本数据类型
- HTTP之Cookie
- 代码结构
- iOS 开发之 点击tabbarItem添加是否跳转登录页面判断
- OpenGL 纹理本质