题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5907
记下每块大小,然后n*(n+1)/2
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 typedef long long LL; 5 const int maxn = 100100; 6 char s[maxn]; 7 vector<LL> ret; 8 9 int main() { 10 // freopen("in", "r", stdin); 11 int T; 12 scanf("%d", &T); 13 while(T--) { 14 ret.clear(); 15 scanf("%s", s); 16 int pos = 0; 17 bool flag = 0; 18 for(int i = 0; s[i]; i++) { 19 if(s[i] == 'q') pos++; 20 else { 21 if(pos != 0) ret.push_back(pos); 22 pos = 0; 23 } 24 } 25 if(pos != 0) ret.push_back(pos); 26 LL ans = 0; 27 for(int i = 0; i < ret.size(); i++) { 28 ans += ret[i]*(ret[i]+1)/2; 29 } 30 cout << ans << endl; 31 } 32 return 0; 33 }