A Count Task

A Count Task

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)<=105,1<=∑len(string)<=105
Strings only contain lowercase English letters.

题意大概就是现有一串全为小写字母的字符串求其中字串只含一种小写字母的所有情况。
思路,从前往后暴力因为只含一种字母所以我们只需要寻找最长的连续相同的字母设其长为l那么这一子段字串的个数就是(l+1)*l/2;就是以1为首项1为公差的等差序列的和(eg,长度为l的串有一个 长度为l-1的串有2个类推到长度为1的字串有l个)。还需要注意要用longlong
ac

#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
int v[20089],s[2],m;
int dp[20089][50];
int main(){
    long long int n,s,sum;
    char x;
    string a;
    cin>>n;
    while(n--)
    {
        s=0,sum=0;
        cin>>a;
        a+='0';
        x=a[0];
        for(int i=0;i<a.size();i++)
        {
            if(a[i]==x)
            {
                s++;
            }
            else
            {
                sum+=s*(s+1)/2;
                x=a[i];
                s=1;
            }
        }
        cout<<sum<<endl;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值