A Count Task HDU - 6480

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.

Input

The input starts with one line contains exactly one positive integer TT 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.

Output

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

Sample Input

3
qwertyuiop
qqwweerrttyyuuiioopp
aaaaaaaaaa

Sample Output

10
30
55

Hint

1<=T<=20,1<=len(string)<=10^5,1<=∑len(string)<=10^5
Strings only contain lowercase English letters.

题目大意:给一个全部为小写字母的字符串,求其中字符串只含有一种小写字母的所有情况总和。

思路:直接暴力一边循环遍历即可,查找循环字段的长度,进而可以知道子含有这一个只含有一种字母字符串段的所有情况和。由前n项=(1+n)*n/2,一次加起来即可;要注意使用long long。

代码如下:

#include <iostream>
#include <stdio.h>
#include<string.h>
#include <algorithm>
#define ll long long
using namespace std;
#define inf 0x3f3f3f3f
char a[100010];
int i,j,k;
int n,m;
ll ans;
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s",&a);
        int len=strlen(a);
        ll cnt;
        ans=0;
        for(i=0; i<len; i++)
        {
            cnt=1;
            while(i+1<len)//查找重复字段
            {
                if(a[i]==a[i+1])
                {
                    cnt++;
                    i++;
                }
                else
                    break;//如若不在连续重复,即跳出循环
            }
            ans+=cnt*(cnt+1)/2;//计算前cnt个数的和。
        }
        printf("%lld\n",ans);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值