D - Counting Test Gym - 101532D 字符串
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
1
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 + ;
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 字符串的更多相关文章
- Gym - 101532D Counting Test 前缀和统计字符串
题意:给你一个1e4长的字符串S,有1e5个询问,每个询问形如 l r c ,其中l,r为左右边界,c为所询问的字符.注意,l,r,可以大于串S的长度,这种情况下认为S自身重复无数次(S+S+S··· ...
- Gym 100989E 字符串
Description standard input/output Islam is usually in a hurry. He often types his passwords incorrec ...
- Postgresql 字符串操作函数
样例测试: update property set memorial_no = btrim(memorial_no, ' ') where memorial_no like ' %' 或:update ...
- Postgresql数据库的一些字符串操作函数
今天做项目遇到客户反映了一个麻烦的事情,有一些数据存在,但就是在程序中搜索不出来,后来分析,发现问题为数据前面有几个空白字符,后来用SQL查询了一下,发现八九个数据表中,数千万条数据中有将近三百万条数 ...
- postgres函数
1.数据修复最先考虑通过db内做修复,实在不行,在考虑外部应用程序通过jdbc修复. 比如一个场景:profile_image_url与enlarge_image_url都是微博用户信息返回的字段. ...
- 《Think Python》第8章学习笔记
目录 8.1 字符串是一个序列(A string is a sequence) 8.2 len 8.3 用一个 for 循环进行遍历(Traversal with a for loop) 8.4 字符 ...
- PostgreSQL tips
tip 1 在sql中我们可以设置一个列自增长identity(1,1),但在postgresql中却没有这个关键字定义.但postgresql也有实现相关功能,那就是只需要将该列数据类型标记为ser ...
- 莫逸风CSDN文章目录
『Ⅱ』-----随笔 莫逸风CSDN文章目录 The Programmer's Oath程序员的誓言-- 今天突发奇想写了一个小工具,CSDN文章目录生成器 vue去掉一些烦人的校验规则 输入npm ...
- ACM: Gym 100935B Weird Cryptography - 简单的字符串处理
Weird Cryptography Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
随机推荐
- SpringBoot之前端文件管理
WebJars能使Maven的依赖管理支持OSS的JavaScript库/CSS库,比如jQuery.Bootstrap等. (1)添加js或者css库 pom.xml <dependency& ...
- 爬虫应对js混淆的方法
大家做爬虫可能经常要跟js打交道.如果积累一定的经验肯定会遇到eval(....);这种js,很多新人可能慌了,woc这怎么办??????? 下面楼主给大家介绍一种方法简单,有效. F12 在Cons ...
- .Net C# 使用Redis
Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.从2010年3月15日起,Redis的开发工作由VMware主 ...
- C#对接----韵达开发平台--取电子面单
引子 最近根据业务的一些需求,所以放弃从快递鸟对接去电子面单,转而直接对接韵达开发平台:http://open.yundasys.com/ ,中间踩了一些坑,借此做了一个小案例给大伙,瞅瞅,若有需改进 ...
- struts2_struts.xml配置文件讲解
1.bean Bean详细讲解:https://www.cnblogs.com/lulu638/p/4340703.html 2.constant constant属性配置,可配置的属性可以参考def ...
- Java学习笔记之——多态、抽象
1. 多态 多态:同一种事物调用同一个方法有不同的表现行为.(同一类型操作,作用于某一类对象,可以有不同的解释,产生不同的执行结果) 应用场景;当你定义一个功能性的方法可以使用多态的概念 前提:子类继 ...
- 8张图让你一步步看清 async/await 和 promise 的执行顺序
摘要: 面试必问 原文:8张图帮你一步步看清 async/await 和 promise 的执行顺序 作者:ziwei3749 Fundebug经授权转载,版权归原作者所有. 为什么写这篇文章? 说实 ...
- angular 去掉url里面的#
1.适合客户端的方法,但是页面不能刷新,一刷新就404 (1)在index.html里添加 <base href="/"> (2)在app.js的config里,注入$ ...
- express 连接数据库
(1)创建项目 ,项目名cntMongodb express -e cntMongodbcd cntMonfodbnpm installnpm install mongoose --save //安装 ...
- cf1139D. Steps to One(dp)
题意 题目链接 从\([1, M]\)中随机选数,问使得所有数gcd=1的期望步数 Sol 一个很显然的思路是设\(f[i]\)表示当前数为\(i\),期望的操作轮数,转移的时候直接枚举gcd \(f ...