水题-find q

来源:互联网 发布:儿童电脑画图软件 编辑:程序博客网 时间:2024/05/17 22:30
Byteasar is addicted to the English letter 'q'. Now he comes across a string SSconsisting of lowercase English letters. 

He wants to find all the continous substrings of SS, 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)T(1≤T≤10), denoting the number of test cases. 

In each test case, there is a string SS, it is guaranteed that SS only contains lowercase letters and the length of SS is no more than 100000100000.
Output
For each test case, print a line with an integer, denoting the number of continous substrings of SS, which only contain the letter 'q'.

Sample Input
2qoderquailtyqqq
Sample Output
1

7

这道题其实不难,但是本人是个菜鸟,超时了好几次。先解释一下题意,在已知的字符串中寻找只含有q的

子字符串,这个规律其实很好找,找到规律后就很简单了,为了不超时,可以用数组打一个简单的表,直

接提取就好了,这个和那个汉罗塔的变式很相似,废话不多说,下面上代码。

#include<cstdio>  #include<cstring>  #include<iostream>  #define N 100005  using namespace std;  int main()  {      int i,t;      long long sum[N];      char s[N];      sum[1]=1;      for(i=2;i<=N;i++)          sum[i]=sum[i-1]+i;      scanf("%d",&t);      while(t--)      {          int cnt=0,len;          long long ass=0;          scanf("%s",s+1);          len=strlen(s+1);          for(i=1;i<=len+1;i++)          {              if(s[i]=='q') cnt++;              else              {                  ass=ass+sum[cnt];                  cnt=0;              }          }          printf("%lld\n",ass);      }      return 0;  }  

0 0
原创粉丝点击