A - A Count Task HDU - 6480

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.

Input

The input starts with one line contains exactly one positive integer TT 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.

Output

For each test case, output one line containing “y” where y is the number of target substrings.

Sample Input

3
qwertyuiop
qqwweerrttyyuuiioopp
aaaaaaaaaa

Sample Output

10
30
55


        
  

Hint

1<=T<=20,1<=len(string)<=10^5,1<=∑len(string)<=10^5
Strings only contain lowercase English letters.

        
 

需要注意的地方

这个明白题意就不难理解了。

#include"stdio.h"
#include"iostream"
#include"algorithm"
#include"cstring"
using namespace std;
const int maxn = 100005;
long long a[100005];

int main()
{
	a[0]=0;
	for(int i = 1;i < maxn; i ++) 
	{
		a[i]=a[i-1]+i;
	}
	int t;
	cin >> t;
	while(t--)
	{
		char c[100005];
		cin >> c;
		int len = strlen(c);
		if(len==1)
		{
			cout<<1<<endl;
		}
		int flag1=0;
		long long sum = 0;
		for(int i = 1;i <  len ;i ++)
		{
			if(i == len-1)
			{
				if(c[i]==c[i-1])
				{
					sum+=a[i-flag1+1];
				}
				else
				{
					sum+=2;
				}
				break;
			}
			if(c[i]!= c[i-1])
			{
			//	cout<<"!!!!"<<i-flag1<<endl;
				sum+=a[i-flag1];
				flag1=i;
			}
		}
		cout<<sum<<endl;
	}
	return 0;
}
发布了58 篇原创文章 · 获赞 20 · 访问量 5218

猜你喜欢

转载自blog.csdn.net/qq_41658124/article/details/103196083