Description CA loves “6” very much. Now CA has a string S which consists of N digits. Now, she wonders that there are how many non-empty strings are substring of S and only contain number “6”. Input First line contains T denoting the number of testcases. T testcases follow. Each testcase contains a integer in the first line, denoting N, the length of string S. The second line is a string whose length is N. 1≤T≤10, 2≤N≤200000 Output For each test case, output answer. Sample Input 2
3
616
3
166
Sample Output
2
3
讲真这种题真不应该写个博客,但是重点就是没有想到用int
类型计算得到long long的过程中可能会爆炸,是我太菜了,发个博记录下。
#include<bits/stdc++.h>
#define input freopen("input.txt","r",stdin)
using namespace std;
int main(){
//freopen("input.txt","r",stdin);
int n,i,j,t,start,end,flag;
long long sum;
string s;
scanf("%d",&t);
while(t--){
sum=0;
scanf("%d",&n);
cin>>s;
s+='0';
start=end=0;
for(flag=0,i=0;i<=n;i++){
if(flag==0&&s[i]=='6'){
flag=1;
start=i;
}
else if(flag==1&&s[i]!='6'){
flag=0;
sum+=(long long)((1+(long long)(i-start))*(i-start)/2); //就是这!
}
}
cout<<sum<<endl;
}
return 0;
}