本文主要是介绍poj 2887 块状数组,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 7116 | Accepted: 1701 |
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 canassume(承担) that it is non-empty and its length does notexceed(超过) 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. Ifp 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 thep-th character exists.
All characters in the input(投入) aredigits(数字) orlowercase(小写字母) letters of the Englishalphabet(字母表).
Output
For each Q command output(输出) one line containing only the single character queried.
Sample Input
题意 :
有一串字符串 需要进行一些操作 ;当输入Q i时 将第i个字符输出
当输入 I ch i 时 将第 i个字符前添加一个字符ch;
这道题 本来想用 线段树来做的 后来感觉 块状数组更简单一些。
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
#include<string.h>
#include<stdio.h>
#include<algorithm>
#define maxn 1001
#define maxl 2000005
#define N 1001
using namespace std;
char c[maxn*maxn];
char a[maxn][maxn*3];
int len[N];
int alen;
char query(int k){int cou=0;for(int i=0;i<alen;i++){if(k<=cou+len[i]){return a[i][k-cou-1];}cou+=len[i];}
}
void add(int k,char ch){int cou=0;int r;for(int i=0;i<alen;i++){if(k<=cou+len[i]){r=i;break;}if(i==alen-1){r=alen-1;break;}cou+=len[i]; }int pos=k-cou-1;if(pos>=len[r])pos=len[r];for(int i=len[r];i>pos;i--){a[r][i]=a[r][i-1];}a[r][pos]=ch;len[r]++;}int main(){while(~scanf("%s",c)){int slen=strlen(c);int along;memset(len,0,sizeof(len));along=(slen+999)/1000;alen=(slen+along-1)/along;for(int i=0;i<alen;i++)len[i]=along;for(int i=0;i<slen;i++){a[i/along][i%along]=c[i];}len[alen-1]=(slen-1)%along+1;int n;scanf("%d",&n);while(n--){char op[3];int k;scanf("%s",op);if(op[0]=='Q'){scanf("%d",&k);printf("%c\n",query(k));} else{scanf("%s %d", op, &k); add(k,op[0]);}}
}
return 0;
}
这篇关于poj 2887 块状数组的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!