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

 

全部评论

相关推荐

浪子陪都:简历最优秀的地方放到了后面,国奖,校级奖学金这些是最亮眼的。说明你跟同级别的学生不一样。 建议台灯这个,PCB布局布线这个词汇不专业,业内是PCB Layout,第二,单片机的板子一般不用考虑SI,PI 都是低速信号,只要遵循3W原则就好了。 单片机的项目太low了,技能这块,你要看一下BOSS直聘的招聘要求,按照别人的要求写,一些关键词可以增加你简历被检索到的概率。 主修课程不用写,这个没有人去关注的。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务