A Count Task(类型转换)

 A: A Count Task

时间限制: 1 Sec  内存限制: 128 MB
提交: 113  解决: 45
[提交] [状态] [讨论版] [命题人:admin]

题目描述

Count is one of WNJXYK’s favorite tasks. Recently, he had a very long string and he wondered that how many substrings which contains exactly one kind of lowercase in this long string. But this string is so long that he had kept counting for several days. His friend Kayaking wants to help him, so he turns to you for help. 

 

输入

The input starts with one line contains exactly one positive integer T which is the number of test cases.
Each test case contains one line with a string which you need to do a counting task on.

 

输出

For each test case, output one line containing “y” where y is the number of target substrings.

 

 

样例输入

3
qwertyuiop
qqwweerrttyyuuiioopp
aaaaaaaaaa

 

样例输出

10
30
55

 

提示

1≤T≤20,1≤len(String)≤10^5,1≤∑len(string)≤10^6
Strings only contain lowercase English letters.

 

 

 

照题意,简单得出ans = \sum(1+cnt)*cnt/2(cnt为每个连续的相同字母的个数)

 

然后5min左右写完程序了,一交,wa,思索半天,觉得很对,改了改其他小地方,wa

1h后,突然顿悟ll ans = a*b(a,b为整型),爆炸,

为啥我记得是a*b会以长整型运算然后再变成long long啊qwq

这是wa的代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
char a[1000006];
int main()
{
    int t;

    scanf("%d",&t);

    while(t--)
    {
        memset(a,0,sizeof(a));
        scanf("%s",a);
        ll ans = 0;
        for(int i=0;a[i];i++)
        {
            ///printf("%c",a[i]);
            int cnt = 1;

            while(a[i+1]&&a[i]==a[i+1])cnt++,i++;

            ans += (1+cnt)*cnt/2;

        }
        printf("%lld\n",ans);
    }

    return 0;
}

 

这是AC的代码

 

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
char a[1000006];
int main()
{
    int t;

    scanf("%d",&t);

    while(t--)
    {
        memset(a,0,sizeof(a));
        scanf("%s",a);
        ll ans = 0;
        for(int i=0;a[i];i++)
        {
            ///printf("%c",a[i]);
            ll cnt = 1;

            while(a[i+1]&&a[i]==a[i+1])cnt++,i++;

            ans += (1+cnt)*cnt/2;

        }
        printf("%lld\n",ans);
    }

    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值