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
Input1Output
8 5
abcabdca
1 8 c
1 15 b
4 9 a
5 25 d
2 7 c
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; }