隐藏页面特效

POJ 2887 Big String

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

题目大意 

给一个字符串,长度不超过 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; }

 

 

 


__EOF__

本文作者shenben
本文链接https://www.cnblogs.com/shenben/p/6270672.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   神犇(shenben)  阅读(242)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示