Big String
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 7053   Accepted: 1684

Description

You are given a string and supposed to do some string manipulations.

Input

The first line of the input contains the initial string. You can assume that it is non-empty and its length does not exceed 1,000,000.

The second line contains the number of manipulation commands N (0 < N ≤ 2,000). The following N lines describe a command each. The commands are in one of the two formats below:

  1. I ch p: Insert a character ch before the p-th character of the current string. If p is larger than the length of the string, the character is appended to the end of the string.
  2. Q p: Query the p-th character of the current string. The input ensures that the p-th character exists.

All characters in the input are digits or lowercase letters of the English alphabet.

Output

For each Q command output one line containing only the single character queried.

Sample Input

ab
7
Q 1
I c 2
I d 4
I e 2
Q 5
I f 1
Q 3

Sample Output

a
d
e

Source

POJ Monthly--2006.07.30, zhucheng

题目大意

给一个字符串,长度不超过 106,有两种操作:

1、Q x(0 < x <= len(s)) 表示查询当前串中第x个字符

2、I c x(c为字母 0 < x <= len(s)+1)表示在第x个位置插入c字符 x == len+1表示在串尾插入

操作的总数不超过 2000

做法分析

    块状链表裸题。详见代码

#include<cstdio>
#include<cstring>
#include<iostream>
#define m(s) memset(s,0,sizeof s)
using namespace std;
const int N=1010;
int l[N],n,m;
char s[N*N],eg[N][N*3];
void insert(int x,char c){int n1=0,p1,pn=n;for(int i=1;i<=n;i++){if(n1+l[i]>=x){pn=i;break;}if(i==n) break;n1+=l[i];}p1=x-n1;l[pn]=max(p1,l[pn]+1);for(int i=l[pn];i>p1;i--) eg[pn][i]=eg[pn][i-1];eg[pn][p1]=c;
}
void query(int x){int n1=0,p1,pn=n;for(int i=1;i<=n;i++){if(n1+l[i]>=x){pn=i;break;}n1+=l[i];}p1=x-n1;printf("%c\n",eg[pn][p1]);
}
void work(){scanf("%d",&m);int len=strlen(s),ave;ave=(len+999)/1000;n=(len-1)/ave+1;for(int i=0;i<len;i++) eg[i/ave+1][i%ave+1]=s[i],l[i/ave+1]++;while(m--){char c[2];int x;scanf("%s",c);if(c[0]=='I') scanf("%s%d",c,&x),insert(x,c[0]);else scanf("%d",&x),query(x);}
}
int main(){while(scanf("%s",s)==1){m(l);m(eg);work();} return 0;
}

 

 

转载于:https://www.cnblogs.com/shenben/p/6270672.html

POJ 2887 Big String相关推荐

  1. oracle rman备份时间点,Oracle Rman 控制RMAN的备份时间,减少IO消耗

    一.问题描述 由于服务器配置不高,备份策略为周末全备.周一至周六差异备份. 平时服务器CPU使用30%左右. 全备份时,开启两个通道,CPU达到70%-80%左右,业务不卡顿.不掉单,session不 ...

  2. Poj 2887-Big String Splay

    题目:http://poj.org/problem?id=2887 Big String Time Limit: 1000MS   Memory Limit: 131072K Total Submis ...

  3. POJ 1002 487-3279

    2019独角兽企业重金招聘Python工程师标准>>> 487-3279 Time Limit: 2000MS Memory Limit: 65536K Total Submissi ...

  4. 字符串匹配のKMP【专题@AbandonZHANG】

    算法详解 很长时间内都没有能够很理解KMP算法的精髓,尤其是很多书上包括<算法导论>没有把next函数(亦或 π函数)讲解的很透彻. 今天去看了matrix67大牛博客中关于kmp部分的讲 ...

  5. 字符串匹配のKMP【@Abandon】

    算法详解 很长时间内都没有能够很理解KMP算法的精髓,尤其是很多书上包括<算法导论>没有把next函数(亦或 π函数)讲解的很透彻. 今天去看了matrix67大牛博客中关于kmp部分的讲 ...

  6. Java知识——精华总结

    Java知识--精华总结 一.java概述与基础知识 1.何为编程? 编程就是让计算机为解决某个问题而使用某种程序设计语言编写程序代码,并最终得到结果的过程. 为了使计算机能够理解人的意图,人类就必须 ...

  7. 【贪心】Sunscreen(poj 3614/luogu 2887)

    Sunscreen poj 3614 luogu 2887 题目大意: 有n个人,每个人要求选一个价值在minniminn_iminni​到maxximaxx_imaxxi​的物品,现在有m件物品,每 ...

  8. POJ 3268 D-Silver Cow Party

    http://poj.org/problem?id=3268 Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently ...

  9. POJ 2226 Muddy Fields 最小点覆盖+加建图(好题)

    题目链接 题目一看就是最小点覆盖,这道题与POJ - 3041 算是一类题,但是3041算是一道十分裸的,因为删除的是整行或者整列,所以图其实是现成的,但是本题的难点就在如何建图. 思路:首先还是尽量 ...

最新文章

  1. 【Java】剑指 Offer 52. 两个链表的第一个公共节点
  2. Linux日志系统-07:案例3-rsyslog+logrotate实现SSH的日志滚动
  3. thinkphp html php文件,ThinkPHP生成静态HTML文件
  4. NodeJs实现下载Excel文件
  5. Spring精华问答 | Spring Boot有哪些优点?
  6. 网页统计所用到的名词解析
  7. ES6-使用let关键字定义变量
  8. Poj 2001 Shortest Prefix(字典树模板)
  9. vs C4996的错误解决方法
  10. c语言中宏名的作用时段,C语言中的宏定义!
  11. 爬取西刺代理的免费IP
  12. 前后端分离前端框架的主要内容是什么?
  13. 画地貌图matlab
  14. DP/最短路 URAL 1741 Communication Fiend
  15. mysql插入成功返回主键_MyBatis + MySQL返回插入成功后的主键id
  16. Java 多态的薪酬计算的练习
  17. js设计模式——组合模式
  18. 问题分析工具 - 3 legged 5 why analysis
  19. IntelliJ IDEA 安装和使用
  20. DockerFile---构建docker镜像的文件

热门文章

  1. 竟然被尤雨溪点赞了:我给Vue生态贡献代码的这一年
  2. 面试字节跳动后的2点总结,建议收藏!
  3. 35 点击全图后发现地图“不见了”
  4. 想要设计自己的微服务?看这篇文章就对了 1
  5. Python 模块之 string.py
  6. Tensorflow之安装
  7. iOS c语言 基本运算符
  8. Spring + hibernate + JPA 配置
  9. sessionfunctionphp实战第六天
  10. linux 后台一直执行的sh