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;
}
/**/

 

全部评论

相关推荐

11-18 15:57
门头沟学院 Java
最终归宿是测开:这个重邮的大佬在重邮很有名的,他就喜欢打92的脸,越有人质疑他,他越觉得爽😂
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务