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. idea: Unable to parse template "class"

    使用idea创建文件时,报“Cannot Create Class”.具体错误为: Unable to parse template "Class" error meesage: ...

  2. ajax实现跨域访问

    ajax跨域访问是一个老生畅谈的问题啦,网上解决方法很多,discuz用的p3p协议,有兴趣的朋友可以了解下,比较常用的是JSONP方法,貌似目前这种方法只支持GET方式,不如POST方式安全. 即使 ...

  3. springMVC_10拦截器

    一,简介 拦截器概念和struts概念一致 实现拦截器 实现HandlerInterceptor接口 配置拦截器 <mvc:interceptors> <mvc:intercepto ...

  4. Java集合性能分析-疯狂Java讲义

    一.各Set实现类的性能分析 HashSet和TreeSet是Set的两个典型实现.HashSet的性能总是比TreeSet好(特别是最常用的添加.查询元素等操作),因为TreeSet需要额外的红黑树 ...

  5. JavaScript中的三种弹窗

    ---恢复内容开始--- 1.警告窗口(alert) <script type="text/javascript"> alert("我是警告弹框!" ...

  6. hihocoder编程练习赛75

    题目1 : 工作城市分配 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 H公司在北京和上海两个城市各有一间办公室.该公司最近新招募了2N名员工,小Hi负责把这2N名员工 ...

  7. CSS用法总结(持续更新)

    一.html,body{height:100%} 解决了容器高度不足(容器高度由子元素高度决定,而%按照父元素的百分比),无法用%布局页面的问题 把html和body的高度设置为浏览器高度,此时会出现 ...

  8. Android为TV端助力 SharedPreferences 轻量级存储!

    首先在当前进程也就是当前的项目里面进行存储 SharedPreferences.Editor editor = mContext.getSharedPreferences("tvplay&q ...

  9. 利用顶点位移进行VR畸变校正

    VR开发的最大挑战之一是对高帧率与高分辨率结合的要求.我们通过把顶点转化为“镜头空间”,删除了需要全屏渲染的纹理,这样就可以大规模提高手机性能. 下面的技术使用谷歌的Cardboard Unity S ...

  10. 使用Java实现简单的局域网设备扫描

    在产品的使用中我们一般都要设置一个配置环节,这个环节可以设定主机的IP地址等信息,但是这样配置的话使得我们的产品用起来效果不是很好,因此我想到了实现局域网IP扫描的功能,IP局域网扫描是指定IP网段获 ...