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. 第一讲 从头开始做一个web qq 机器人,第一步获取smart qq二维码

    新手教程: 前言:最近在看了一下很久很久以前做的qq机器人失效了,最近也在换工作目前还在职,时间很挺宽裕的.就决定从新搞一个web qq机器人 PC的协议解析出来有点费时间以后再做. 准备工作: 编译 ...

  2. [javaEE] Tomcat的安装与配置

    下载压缩包,解压缩,好,安装完成 进入解压目录/bin/下面,找到startup.bat,双击,此时如果报错,那么就是没有设置环境变量JAVA_HOME,进入环境变量去设置,JAVA_HOME指向jd ...

  3. OpenOffice安装和转换乱码解决方案

      前言: OpenOffice项目中用途:word转换pdf Windows安装.转换:安装包下载后一路OK 就可以正常安装,转换没有问题 Linux安装.转换:安装有分DEB包和RPM包,下面会说 ...

  4. linux下ftp服务器搭建

    1.yum install vsftpd  使用yum安装ftp 2.创建并授权ftp文件目录   mkdir -P /ftp/ftpadmin       chmod -R 777 /ftp/ftp ...

  5. JS预解析机制

    JS的预解析过程: 1,预解析 2,再逐行解读代码, 实例: ---------------------------- <script>        var name="xm& ...

  6. PHP微信H5支付

    今天项目用到了微信新出的h5支付直接去官网 https://pay.weixin.qq.com/wiki/doc/api/index.html找dome去了找了之后才发现没有一脸懵逼,一开始以为和公众 ...

  7. JAVA 多线程(1):synchronized

    入坑3年,对线程总是一知半解,最多停留在copy,决定还是仔细看看这方面的东西,一点点的记录让自己理解,对一些重要的概念进行记录和理解(包括参考作者的原话与个人理解) 参考链接:https://www ...

  8. js 格式化数字(每三位加逗号)

    // 方法一 unction toThousands(num) { var result = [ ], counter = 0; num = (num || 0).toString().split(' ...

  9. layui layer select 选择被遮挡

    在编程的时候, layer弹窗中的select 经常会遇到被遮挡的情况: 解决方法:在页面里面找到对应div的class 给overflow新的属性visible即可(默认值.内容不会被修剪,会呈现在 ...

  10. BDD实战篇 - 在.NET Core下安装Specflow

    这是<如何用ABP框架快速完成项目 >系列中的一篇文章. BDD很赞!比TDD先进很多,能够大大提高编码效率. 让我们动手起来吧!先在.NET Core下安装Specflow! 官网教程在 ...