Description

Sample Input


6
JANJETINA
5
1 J
1 A
6 N
6 I
5 E

Sample Output


1
0
2
1
1

题意:给你一个长度小于等于10^6的字符串,然后每次让它循环铺盖,构成层数为n的塔,让你求得第i层塔中某个字符的个数。

思路:首先要注意到字符串是从左到右覆盖该行和从右到做覆盖该行不影响结果,所以我们全部考虑为从左到右覆盖。我们先遍历一遍字符串,用vector<int>vect[30]记录字符为i的所有位置。然后我们看c是不是能被字符串的长度len整除,如果能,那么答案就是vec[c-'A'+1].size()*c/len,如果不能整除,那么我们就要把余下的部分算完。当我们把中间的整段字符串都去掉的时候,余下的部分可能是前面一串,或者后面一串,或者前面后面都有剩下的,这里一开始我直接算前一串的开头到len-1中的数量加上后一串的0到结尾的数量,但是wa了,因为这样的想法是错误的。因为我这样算可能会包含已经算过的,比如abcdefabcdef,如果我选第2个b和倒数第2个e,那么我这样算的话,会包含之前算过的,因为两个字符间的距离大于len了。所以我们要采用别的方法,起始点q固定,尾节点变为(q+(c%len))%len,那么起始点和尾节点之间的距离一定小于len了,这样就不会重复算了。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ldb;
#define inf 99999999
#define pi acos(-1.0)
#define maxn 1000050
vector<int>vec[30];
vector<int>::iterator it;
int len;
int chuli(int pos,int bianhao)
{
int i,j;
int p;
p=upper_bound(vec[bianhao].begin(),vec[bianhao].end(),pos)-vec[bianhao].begin();
return p;
}
int getkaitou(ll ceng)
{
int i,j;
ll sum;
if(ceng==1)return 0;
if(ceng%2==0)sum=( (ceng/2%len)*((ceng-1)%len)+1 )%len;
else sum=( (ceng%len)*((ceng-1)/2%len)+1 )%len;
if(sum==0)sum=len-1;
else sum--;
return sum;
}
char s[maxn]; int main()
{
int m,i,j;
ll n,c;
while(scanf("%lld",&n)!=EOF)
{
scanf("%s",s);
len=strlen(s);
for(i=1;i<=26;i++)vec[i].clear(); for(i=0;i<len;i++){
vec[s[i]-'A'+1].push_back(i);
}
char str[10];
int p;
ll sum;
scanf("%d",&m);
while(m--){
scanf("%lld%s",&c,str);
int bianhao=str[0]-'A'+1;
int kaitou=getkaitou(c);
if(c%len==0){
printf("%lld\n",(ll)vec[bianhao].size()*(ll)(c/len) );
continue;
}
ll beishu=c/len;
sum=0;
sum+=(ll)beishu*(ll)vec[bianhao].size();
ll jianju=c%len;
int jiewei=(kaitou+jianju-1)%len;
if(kaitou<=jiewei){
printf("%lld\n",sum+chuli(jiewei,bianhao)-chuli(kaitou-1,bianhao) );
}
else{
printf("%lld\n",sum+chuli(jiewei,bianhao)+chuli(len-1,bianhao)-chuli(kaitou-1,bianhao) );
}
}
}
return 0;
}

zjnu1730 PIRAMIDA(字符串,模拟)的更多相关文章

  1. 用字符串模拟两个大数相加——java实现

    问题: 大数相加不能直接使用基本的int类型,因为int可以表示的整数有限,不能满足大数的要求.可以使用字符串来表示大数,模拟大数相加的过程. 思路: 1.反转两个字符串,便于从低位到高位相加和最高位 ...

  2. HDU-3787(字符串模拟)

    Problem Description 给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号","隔开.现在请计算A+B的结果,并以正常形式输出.   Input 输入包含 ...

  3. HDU-1002.大数相加(字符串模拟)

    本题大意:给出两个1000位以内的大数a 和b,让你计算a + b的值. 本题思路:字符串模拟就能过,会Java的大佬应该不会点进来...... 参考代码: #include <cstdio&g ...

  4. Codeforces Round #425 (Div. 2) B. Petya and Exam(字符串模拟 水)

    题目链接:http://codeforces.com/contest/832/problem/B B. Petya and Exam time limit per test 2 seconds mem ...

  5. HDU-Digital Roots(思维+大数字符串模拟)

    The digital root of a positive integer is found by summing the digits of the integer. If the resulti ...

  6. Vigenère密码 2012年NOIP全国联赛提高组(字符串模拟)

    P1079 Vigenère 密码 题目描述 16 世纪法国外交家 Blaise de Vigenère 设计了一种多表密码加密算法――Vigenère 密 码.Vigenère 密码的加密解密算法简 ...

  7. CCF(JSON查询:40分):字符串+模拟

    JSON查询 201709-3 纯字符串模拟,考的就是耐心和细心.可惜这两样我都缺... #include<iostream> #include<cstdio> #includ ...

  8. codeforces 723B Text Document Analysis(字符串模拟,)

    题目链接:http://codeforces.com/problemset/problem/723/B 题目大意: 输入n,给出n个字符的字符串,字符串由 英文字母(大小写都包括). 下划线'_' . ...

  9. Codeforces 1090B - LaTeX Expert - [字符串模拟][2018-2019 Russia Open High School Programming Contest Problem B]

    题目链接:https://codeforces.com/contest/1090/problem/B Examplesstandard input The most famous characters ...

随机推荐

  1. Mac使用HomeBrew

    前言 考虑许久终于决定入手mac耍耍,还是因为要找工作了,手上的win本大学入的,现在使用卡顿太多,另外就是mac作为程序员之友仰慕已久.决定在PDD入了.到手后发现mac真的跟win有很大差别.还是 ...

  2. win7安装oracle11g和oracle client和pl/sql

    一.安装oracle11g 1.下载Oracle 11g R2 for Windows的版本 下载地址:hhttps://www.oracle.com/technetwork/database/ent ...

  3. 【RAC】安装cluster软件 在节点2执行root.sh脚本

    安装cluster软件  在节点2执行root.sh脚本 报错如下: Running vipca(silent) for configuring nodeapps /db/oracle/product ...

  4. windows下如何安装Python、pandas

    windows下如何安装Python.pandas 本篇主要涵盖以下三部分内容: Python.Pycharm的安装 使用Pycharm创建.运行Python程序 安装pandas 1.Python. ...

  5. 输入12V,输出12V的限流芯片

    随着手机充电电流的提升,和设备的多样化,USB限流芯片就随着需求的增加而越来越多,同时为了更好的保护电子设备,需要进行一路或者多路的负载进行限流. USB限流芯片,5V输入 1, PW1502,常使用 ...

  6. 一种获取context中keys和values的高效方法 | golang

    我们知道,在 golang 中的 context 是一个非常重要的包,保存了代码活动的上下文.我们经常使用 WithValue() 这个方法,来往 context 中 传递一些 key value 数 ...

  7. Ubuntu20.04 安装火狐开发者版本(水狐)步骤

    1. 从Mozilla Firefox Developer Edition webpage下载. 2. 将下载的"tar.bz2"文件解压到指定目录, 例如/opt/firefox ...

  8. nginx proxy pass redirects ignore port

    nginx proxy pass redirects ignore port $host in this order of precedence: host name from the request ...

  9. 服务降级 托底预案 Nginx中使用Lua脚本检测CPU使用率,当达到阀值时开启限流,让用户排队

    https://mp.weixin.qq.com/s/FZAcQQAKomGEe95kln1HCQ 在京东我们是如何做服务降级的 https://mp.weixin.qq.com/s/FZAcQQAK ...

  10. spring 之7种重要设计模式

    Spring中涉及的设计模式总结 1.简单工厂(非23种设计模式中的一种) 实现方式: BeanFactory.Spring中的BeanFactory就是简单工厂模式的体现,根据传入一个唯一的标识来获 ...