POJ 2887 Big String
生活随笔
收集整理的這篇文章主要介紹了
POJ 2887 Big String
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Big String
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.
| 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:
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 3Sample Output
a d eSource
POJ Monthly--2006.07.30, zhucheng題目大意?
給一個字符串,長度不超過 106,有兩種操作:
??? 1、Q x(0 < x <= len(s)) 表示查詢當(dāng)前串中第x個字符
??? 2、I c x(c為字母 0 < x <= len(s)+1)表示在第x個位置插入c字符 x == len+1表示在串尾插入
操作的總數(shù)不超過 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; }?
轉(zhuǎn)載于:https://www.cnblogs.com/shenben/p/6270672.html
總結(jié)
以上是生活随笔為你收集整理的POJ 2887 Big String的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。