您的位置:首页 > 理论基础 > 计算机网络

哈尔滨理工大学软件学院ACM程序设计全国邀请赛(网络同步赛)E 666 By Assassin 坑点水题

2016-12-04 09:19 573 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: