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:
- 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.
- 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 ",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; }