• 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();
    
    }
  • 相关阅读:
    014_Python3 循环语句
    013_Python3 条件控制
    012_Python3 斐波纳契数列 + end 关键字
    011_Python3 集合
    010_Python3 字典
    009_Python3 元组
    008_Python3 列表
    006_Python3 数字(Number)
    005_Python3 运算符
    bzoj3160
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5927564.html
Copyright © 2020-2023  润新知