JiaozhuV5 Substrings | ||||||
| ||||||
Description | ||||||
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). | ||||||
Input | ||||||
There 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 "!". | ||||||
Output | ||||||
For each test case: Line 1. Output the number of substrings of the given string, containing exactly k "JiaozhuV5" (Case-sensitive). | ||||||
Sample Input | ||||||
3 1 JiaozhuV5!! 2 abJiaozhuV5!!JiaozhuV5cd 100 abJiaozhuV5!!JiaozhuV5cd | ||||||
Sample Output | ||||||
3 9 0 | ||||||
Hint | ||||||
In 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 int
char 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);
}
}
/*
1
1
abJiaozhuV5!!JiaozhuV5cdJiaozhuV5ef
*/