ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

D - Counting Test Gym - 101532D 字符串

2019-03-12 15:37:57  阅读:203  来源: 互联网

标签:Mohammad his string Gym queries each Test 101532D line


Yousef has a string s that is used to build a magical string w by repeating the string s infinitely many times. For example, if s = aabbb , then w = aabbbaabbbaabbbaabbb....

Mohammad always claims that his memory is strong, and that his ability to count is very high, so Yousef decided to hold a test for Mohammad, in order to know the truth of his claims.

Yousef will give Mohammad q queries, such that each query consisting of two integers l and r, and a lowercase English letter c. The answer of each query is how many times the letter c appears between the lth and rth letters in string w.

Mohammad must answer all the queries correctly, in order to proof his claims, but currently he is busy finishing his meal. Can you help Mohammad by answering all queries?

Input

The first line contains an integer T, where T is the number of test cases.

The first line of each test case contains two integers n and q (1 ≤ n ≤ 104) (1 ≤ q ≤ 105), where n is the length of string s, and q is the number of queries.

The second line of each test case contains the string s of length n consisting only of lowercase English letters.

Then q lines follow, each line contains two integers l and r and a lowercase English letter c (1 ≤ l ≤ r ≤ 109), giving the queries.

Output

For each query, print a single line containing how many times the letter c appears between the lth and rth letters in string w.

Example

Input
1
8 5
abcabdca
1 8 c
1 15 b
4 9 a
5 25 d
2 7 c
Output
2
4
3
3
2

Note

As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.

 

 

 

这个字符串的处理之前就碰到过,之前没有处理好,这个应该以给你的字符串为基础,然后划成三个部分,一个是有多少个完整的从1到n的区间,然后就是开始的x到n直接有多少符合要求的,最后就是从

0到y%n的所有符合要求的,之前也这么想的,但是没有写出来。。。

 

 

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn = 1e4 + 100;
char s[maxn];
ll sum[maxn][30];

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        ll n, m;
        cin >> n >> m;
        cin >> s + 1;
        for (int i = 1; i <= n; i++)
        {
            for (int j = 0; j < 26; j++)
            {
                sum[i][j] = sum[i - 1][j];
            }
            sum[i][s[i] - 'a'] ++;
        }

        while (m--)
        {
            ll x, y;
            char s1[10];
            scanf("%I64d%I64d%s", &x, &y, s1);
            int num = s1[0] - 'a';
            ll ans = 1ll * (y / n * n - (x + n - 1)/n * n)/n *sum[n][num];//求出这之间有多少个完整的区间,注意要把第一个当成完整的区间
            ans += sum[n][num] - sum[(x%n == 0 ? n : x%n) -1][num];//求出第一个区间从x到n,注意这里要加括号,否则意思就变了
            ans += sum[y%n][num];//求出最后的从0到y%n
            printf("%I64d\n", ans);
        }
    }
    return 0;
}

 

标签:Mohammad,his,string,Gym,queries,each,Test,101532D,line
来源: https://www.cnblogs.com/EchoZQN/p/10517082.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有