zoukankan      html  css  js  c++  java
  • 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();
    
    }
  • 相关阅读:
    CVS使用经验谈(zz from chinaunix.net)
    登黄山
    登黄山之二
    Dennis Ritchie 去世
    观迎客松
    从程序员角度看ELF
    再次回到这里
    异步时钟下跨时钟域信号处理
    Fedora14下的Novas和Synopsys
    Oralce导入\导出
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5927564.html
Copyright © 2011-2022 走看看