hdu----Find Q

来源:互联网 发布:淘宝店铺添加客服 编辑:程序博客网 时间:2024/05/17 22:22
Byteasar is addicted to the English letter 'q'. Now he comes across a stringS consisting of lowercase English letters.

He wants to find all the continous substrings of S, which only contain the letter 'q'. But this string is really really long, so could you please write a program to help him?
 

Input
The first line of the input contains an integer T(1T10), denoting the number of test cases.

In each test case, there is a string S, it is guaranteed that S only contains lowercase letters and the length of S is no more than 100000.
 

Output
For each test case, print a line with an integer, denoting the number of continous substrings ofS, which only contain the letter 'q'.
 

Sample Input
2qoderquailtyqqq
 

Sample Output
1
7

题意:给出一串字符串S,问S中的仅包含字母q的连续子串有几个

#include<cstdio>#include<cstring>char a[100005];int main(){    int n;    long long sum,ans;    scanf("%d",&n);    while(n--)    {        ans=sum=0;        scanf("%s",a);        int len=strlen(a);        for(int i=0; i<=len; i++)        {            if(a[i]=='q')                sum++;            else            {                ans+=(sum+1)*sum/2;                sum=0;            }        }        printf("%lld\n",ans);    }    return 0;}


原创粉丝点击