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();
    
    }
  • 相关阅读:
    js炫酷效果
    程序员的执着
    [心得]docker学习笔记
    [心得笔记]多线程之间的内存可见性问题
    Docker入门
    [心得体会]jvm
    redis学习总结
    [心得]redis集群环境搭建的错误
    Linux安装mysql5.7版本
    Cent OS下安装JDK11
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5927564.html
Copyright © 2011-2022 走看看