HDU 5907 Find Q dp
Find Q
题目连接:
http://acm.hdu.edu.cn/showproblem.php?pid=5907
Description
Byteasar is addicted to the English letter 'q'. Now he comes across a string S 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(1≤T≤10), 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 of S, which only contain the letter 'q'.
Sample Input
2
qoder
quailtyqqq
Sample Output
1
7
Hint
题意
问你有多少个子串,全部只含有q这个字母。
题解:
直接dp就好了,dp[i]表示以i结尾的方案数。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 100007;
long long dp[maxn];
void solve()
{
string s;
cin>>s;
memset(dp,0,sizeof(dp));
for(int i=0;i<s.size();i++)
if(s[i]=='q')dp[i]=dp[i-1]+1;
long long ans = 0;
for(int i=0;i<s.size();i++)
ans+=dp[i];
cout<<ans<<endl;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)solve();
}