A Count Task
题面:
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.InputThe 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.OutputFor each test case, output one line containing “y” where y is the number of target substrings.Sample Input3 qwertyuiop qqwweerrttyyuuiioopp aaaaaaaaaaSample Output
10 30 55Hint
1<=T<=20,1<=len(string)<=10^5,1<=∑len(string)<=10^5 Strings only contain lowercase English letters.
题解:
计算每个单词的对相同的单词段的贡献度,然后累和。
#include <bits/stdc++.h> using namespace std; const int mxn = 1e5+7 ; #define ll long long ll n,m,t,k,ans,cnt; int a[mxn] , b[mxn] , c[2*mxn] , d[mxn] , vis[mxn]; void solve() { string str ; for(cin>>t;t;t--){ cin>>str; ans = 0 ; for(int i=0;i<str.size();i++){ k = 1; while(str[i]==str[i+1] && i<str.size()) k++ , i++ ; ans+=(1+k)*k/2; } cout<<ans<<endl; } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); solve(); }