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-23 15:14
中原工学院 Java
程序员花海_:实习和校招简历正确格式应该是教育背景+实习+项目经历+个人评价 其中项目经历注意要体现业务 实习经历里面的业务更是要自圆其说 简历模板尽可能保持干净整洁 不要太花哨的
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务