poj 2887 块状数组
2017-03-21 16:00
239 查看
Big String
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;
这道题 本来想用 线段树来做的 后来感觉 块状数组更简单一些。
Sample Output
Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 7116 | Accepted: 1701 |
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 ; 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 块状数组/线段树
- POJ 2887 块状数组
- Poj 2887 Big String(块状数组)
- 【POJ 2887】Big String(块状数组)
- [POJ 2887] Big String [块状链表]
- poj 2887 块状链表 单点插入+单点查询
- poj 2887 Big String 块状链表
- POJ-2887(块状链表)
- [POJ 2104] K-th Number (块状数组)
- POJ 2887 Big String(块状链表)
- POJ 2887 Big String(块状链表)
- poj 2887 Big String(块状链表)
- POJ 1547 Clay Bully 结构体数组使用题意理解
- 【原】 POJ 1195 Mobile phones 2D树状数组 解题报告
- POJ 2352 Stars【树状数组】
- POJ 1247 数组首尾求和判定相等 枚举法
- POJ 3067 Japan【一维树状数组】
- POJ 3067 Japan (树状数组)
- POJ 1247 数组首尾求和判定相等 枚举法
- POJ 3067 Japan【树状数组】