1. 程式人生 > >A Count Task(求選取子串的方案數(這子串是連續的),子串中只有一種元素)

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