poj 2887 Big String
来源:互联网 发布:博知教育 编辑:程序博客网 时间:2024/06/06 02:45
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
ab7Q 1I c 2I d 4I e 2Q 5I f 1Q 3
Sample Output
ade
这题是用块链过的,差点被卡时间了,接近700ms。因为我是写的动态分配型的,不会写数组版的。
但这题如果用其他数据结构,貌似会TLE,因为O(nlogn)的建树时间,在n=10^6的情况下,那是伤不起的!
但是说句实话,块链真不适合竞赛写!
代码:
#include<cstdio>#include<iostream>#include<cstring>#include<cstdlib>#define Maxn 1000010using namespace std;char s[Maxn];const int max_size=1000;struct blocklist{ char num[max_size]; int sz; blocklist* next; blocklist():sz(0),next(0){}}*head=NULL;blocklist* get_pos(int &x){ blocklist* ret=head; while(ret&&x>ret->sz){ x-=ret->sz; ret=ret->next; } if(!ret) x=1; return ret;}bool merge(blocklist* pos){ if(!pos||!pos->next) return false; if(pos->sz+pos->next->sz<=max_size){ memmove(pos->num+pos->sz,pos->next->num,sizeof(char)*pos->next->sz); pos->sz+=pos->next->sz; blocklist* u=pos->next; pos->next=u->next; delete u; return true; } return false;}void maintain(){ blocklist* pos=head; while(pos){ merge(pos); pos=pos->next; }}void split(blocklist* pos,int x){ blocklist* tmp=pos->next; pos->next=new blocklist(); pos->next->next=tmp; memmove(pos->next->num,pos->num+x-1,sizeof(char)*(pos->sz-x+1)); pos->next->sz=pos->sz-x+1; pos->sz=x-1;}void insert(int x,int n,char* ch){ blocklist* pos=get_pos(x); if(pos) split(pos,x); else{ pos=new blocklist(); if(!head) head=pos; else{ blocklist* it; for(it=head;it->next;it=it->next); it->next=pos; } } for(int i=0;i<n;i++){ if(pos->sz==max_size){ blocklist* tmp=pos->next; pos->next=new blocklist(); pos->next->next=tmp; pos=pos->next; } pos->num[pos->sz++]=ch[i]; } maintain();}char query(int n){ blocklist* pos=head; while(n>pos->sz){ n-=pos->sz; pos=pos->next; } return pos->num[n-1];}int main(){ char ch[10]; int n,p,x; scanf("%s",s); int len=strlen(s); insert(1,len,s); scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%s",ch); if(ch[0]=='Q'){ scanf("%d",&x); printf("%c\n",query(x)); } else{ scanf("%s%d",ch,&p); insert(p,1,ch); } }return 0;}
- poj 2887 Big String
- POJ 2887 Big String
- poj 2887 Big String
- poj 2887 Big String
- POJ 2887 Big String 笔记
- [POJ 2887] Big String [块状链表]
- 【POJ 2887】Big String 块状链表
- POJ 2887Big String(块状链表)
- 【POJ 2887】Big String(块状数组)
- poj 2887 Big String 块状链表
- Poj 2887 Big String(块状数组)
- POJ 2887 Big String 块状链表
- POJ 2887 Big String 线段树离线
- POJ 2887 Big String(线段树 离线处理)
- poj 2887 big string (块状链表)
- POJ 2887 Big String (线段树解法)
- Big String
- String的Big-Three
- ubuntu使用
- 【leetcode】merge-sorted-array
- 解决unity3d 多平台下完美本地文本文件读取的方法
- UNIX网络编程卷1 回射客户程序 UDP 超时设置
- HDU 3292 No more tricks, Mr Nanguo(佩尔方程,矩阵快速幂)
- poj 2887 Big String
- unix/linux编程实践教程--more命令
- POJ 3463 Sightseeing Dijkstra最短路&最短路-1的路径数(计数)
- makefile write_janseman99_1
- 看看别人家的孩子
- User has no SELECT privilege on V$SESSION(执行计划相关)
- python2 python3语法区别
- Python学习笔记(二)——NumPy
- 简易计算器制作