A Count Task(求选取子串的方案数(这子串是连续的),子串中只有一种元素)

A Count Task

时间限制: 1 Sec  内存限制: 128 MB

题目描述

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.

比如aaaa4个a,那么选一个有4种,选两个有3种,选。。。(连续的!!)和为4+3+2+1,就这样遍历一遍就ok了。

/**/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#include <queue>

typedef long long LL;
using namespace std;

int n;
char s[100005];

int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);

	scanf("%d", &n);
	while(n--){
		scanf("%s", s);
		int len = strlen(s);
		LL ans = 0;
		for (int i = 0; i < len; i++){
			int num = 1;
			char ch = s[i];
			for (i++; i < len; i++){
				if(ch == s[i]) num++;
				else break;
			}
			ans += (LL)(1 + num) * num / 2;//记得long long,不然错的。
			i--;
		}
		printf("%lld\n", ans);
	}

	return 0;
}
/**/

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值