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

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值