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

D - Counting Test Gym - 101532D 字符串的更多相关文章

  1. Gym - 101532D Counting Test 前缀和统计字符串

    题意:给你一个1e4长的字符串S,有1e5个询问,每个询问形如 l r c ,其中l,r为左右边界,c为所询问的字符.注意,l,r,可以大于串S的长度,这种情况下认为S自身重复无数次(S+S+S··· ...

  2. Gym 100989E 字符串

    Description standard input/output Islam is usually in a hurry. He often types his passwords incorrec ...

  3. Postgresql 字符串操作函数

    样例测试: update property set memorial_no = btrim(memorial_no, ' ') where memorial_no like ' %' 或:update ...

  4. Postgresql数据库的一些字符串操作函数

    今天做项目遇到客户反映了一个麻烦的事情,有一些数据存在,但就是在程序中搜索不出来,后来分析,发现问题为数据前面有几个空白字符,后来用SQL查询了一下,发现八九个数据表中,数千万条数据中有将近三百万条数 ...

  5. postgres函数

    1.数据修复最先考虑通过db内做修复,实在不行,在考虑外部应用程序通过jdbc修复. 比如一个场景:profile_image_url与enlarge_image_url都是微博用户信息返回的字段. ...

  6. 《Think Python》第8章学习笔记

    目录 8.1 字符串是一个序列(A string is a sequence) 8.2 len 8.3 用一个 for 循环进行遍历(Traversal with a for loop) 8.4 字符 ...

  7. PostgreSQL tips

    tip 1 在sql中我们可以设置一个列自增长identity(1,1),但在postgresql中却没有这个关键字定义.但postgresql也有实现相关功能,那就是只需要将该列数据类型标记为ser ...

  8. 莫逸风CSDN文章目录

    『Ⅱ』-----随笔 莫逸风CSDN文章目录 The Programmer's Oath程序员的誓言-- 今天突发奇想写了一个小工具,CSDN文章目录生成器 vue去掉一些烦人的校验规则 输入npm ...

  9. ACM: Gym 100935B Weird Cryptography - 简单的字符串处理

    Weird Cryptography Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

随机推荐

  1. WPF 窗口

    在WPF中,经常需要对窗口进行设置,下面讲讲常用的几个设置. 窗口样式 1.无边框窗口 无边框透明窗体 设置 WindowStyle="None"--无边框,如果需要其它按钮,如缩 ...

  2. Linux服务器评测脚本 中文IO脚本简单易懂

    中文版: wget -N --no-check-certificate https://raw.githubusercontent.com/FunctionClub/ZBench/master/ZBe ...

  3. 如何在 Mac上 安裝 .NET Core 2.1 ?

    一.前言 Free. Cross-platform. Open source. A developer platform for building all your apps. --- .net co ...

  4. Java 学习笔记 反射与迭代器

    反射 使用反射获得类 Class cls = Class.forName("全类名") //包括包名 Class cls = xx.Class;//xx代表类名 使用反射获得构造方 ...

  5. JSJ——主数据类型和引用

    变量有两种:primitive主数据类型和引用. Java注重类型.它不会让你做出把长颈鹿类型变量装进兔子类型变量中这种诡异又危险的举动——如果有人对长颈鹿调用“跳跃”这个方法会发生什么悲剧?并且它也 ...

  6. 我是这样搞懂一个神奇的BUG

    摘要: 通过分析用户的行为,才想得到为什么会出现这种情况! 前两天在BearyChat收到这样的一个报警消息: 409 ?Conflict ? 平时很少遇到这样的错误,貌似很严重的样子,吓得我赶紧查看 ...

  7. linux查看用户、创建用户、设置密码、修改用户、删除用户命令

    查看用户 /etc/passwd /etc/shadow id alex ' |passwd --stdin alex # 设置密码,不需要交互 [root@localhost ~]# tail -l ...

  8. 2018-08-24 中文代码之Spring Boot对H2数据库简单查询

    续前文: 中文代码之Spring Boot集成H2内存数据库 在词条中添加英文术语域: @Entity public class 词条 { @Id private long id; private S ...

  9. CSS 定位与Z-index

    position: static   Z-index 固定是0 position: absolute/relative/fixed   Z-index 有效 在层叠显示上,所有static定位元素看作 ...

  10. 开发Spring过程中几个常见异常(一)

    异常一:java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory 原因:未导入有关日志管理的jar包或者未添加到路径. ...