Big String-POJ2887块状数组
| Time Limit: 1000MS | Memory Limit: 131072K | 
|---|
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
给一个字符串,有两种操作,在第k个前面插入一个字符,和查找第k个字符,由于字符串比较的长,但是操作比较都少,所以比较适合分块。
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
const int Max = 2000;//分块的大小
int next[2100];
int Size[2100];
char s[2100][2100];
char str[1000100];
int Num,top;
void Init()
{
    for(int i=0;i<2100;i++)
    {
        next[i]=-1;
        Size[i]=0;
    }
    Num = 0;
}
void Build()
{
    int len =strlen(str),top;
    for(int i=0;i<len;)
    {
        top = Num++;
        for(int j=0;j<Max&&i<len;j++,i++)
        {
            s[top][j] = str[i];
            Size[top]++;
        }
        if(i<len)
        {
            next[top]=Num;
        }
    }
}
void Insert(int st,char c,int num)//插入字符
{
    for(int i = Size[st];i>=num;i--) s[st][i] = s[st][i-1];
    s[st][num-1] = c;
    Size[st]++;
}
void Mer(int i)//当一个块比较的时候,分成两部分,但是加上这个操作,结果就不对,望诸位大神指点一下。
{
    int top=Num++;
    Size[top]  = 0;
    for(int j=Size[i]/2;j<Size[i];j++)
    {
        s[top][j-Size[i]/2] = s[i][j];
        Size[top]++;
        Size[i]--;
    }
    next[top] = next[i];
    next[i] = top;
}
int main()
{
    Init();
    scanf("%s",str);
    Build();
    int n;
    char Op[5],c[5];
    int u;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%s",Op);
        if(Op[0]=='Q')
        {
            scanf("%d",&u);
            int i=0;
            for(i =0;i!=-1&&u>Size[i];i=next[i]) u-=Size[i];
            printf("%c\n",s[i][u-1]);
        }
        else
        {
            scanf("%s %d",c,&u);
            int i = 0;
            for(i=0;next[i]!=-1&&u>Size[i];i = next[i]) u-=Size[i];
            if(u>Size[i])
            {
                u = Size[i]+1;
            } 
            Insert(i,c[0],u);
        }
    }
    return 0;
}
Big String-POJ2887块状数组的更多相关文章
- POJ 2887 Big String (块状数组)
		
题意:给一个字符串(<=1000000)和n个操作(<2000),每个操作可以在某个位置插入一个字符,或者查询该位置的字符.问查询结果. 思路:块状数组. 如果将原来的字符串都存在一起,每 ...
 - Big String   块状数组(或者说平方分割)
		
Big String 给一个字符串,长度不超过 106,有两种操作: 1. 在第 i 个字符的前面添加一个字符 ch 2. 查询第 k 个位置是什么字符 操作的总数不超过 2000 如果直接模拟的话, ...
 - Poj 2887 Big String(块状数组)
		
Big String Time Limit: 1000MS Memory Limit: 131072K Description You are given a string and supposed ...
 - PChar,PAnsiChar,String,AnsiString,Char数组,AnsiChar数组转换
		
PChar,PAnsiChar,String,AnsiString,Char数组,AnsiChar数组之间的转换关系见下图 通过转换链,可以实现任意两个类型之间的互转.如PChar转PAnsiChar ...
 - codefroce D. Powerful array[初识块状数组]
		
codefroce D. Powerful array[初识块状数组] 由于是初始所以,仅仅能先用别人的分析.囧... 题目: 给定一个数列:A1, A2,--,An,定义Ks为区间(l,r)中s出现 ...
 - 字符串的比较【string和字符数组】
		
无论是string 还是 字符数组的字符串比较函数,返回的都是字典序的大小.如 1234 和 5 比较时就是1234的字典序小于5,要想比较字符串表示的数字的大小,需要自己写函数比较
 - Swift 中 String 与 CChar 数组的转换
		
在现阶段Swift的编码中,我们还是有很多场景需要调用一些C函数.在Swift与C的混编中,经常遇到的一个问题就是需要在两者中互相转换字符串.在C语言中,字符串通常是用一个char数组来表示,在Swi ...
 - Swift String转Character数组
		
通过String的characters方法,将String转Character数组 例如: let characters:Array<Character> = Array("01 ...
 - 再谈怎样以最简单的方法将泛型为String类型的集合或String类型的数组转化为逗号间隔字符串形式
		
今天review代码,看见某些大爷在将泛型为String类型的集合或String类型的数组转化为逗号间隔字符串形式时仍然仅仅顾结果不注重过程,"大爷"咱能负点责任吗? 将泛型为St ...
 - C++string,char* 字符数组,int类型之间的转换
		
string.int 常见类型之间相互转换 int & string 之间的转换 C++中更多的是使用流对象来实现类型转换 针对流对象 sstream实现 int,float 类型都可以实现 ...
 
随机推荐
- NOI上看到的几个小学奥数
			
:余数相同问题 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 已知三个正整数 a,b,c. 现有一个大于1的整数x,将其作为除数分别除a,b,c,得到的余数相同 ...
 - QFileSystemModel
			
#include "dialog.h" #include "ui_dialog.h" Dialog::Dialog(QWidget *parent) : QDi ...
 - 关于swap函数传值的问题
			
#include <stdio.h> void swap(int * p3,int * p4); int main() { int a = 9; int b = 8; int * p ...
 - java 基础三 下雪
			
通过repaint()方法进行重画. import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.Graphics; p ...
 - 用Merge来改写相关更新的例子
			
下面的两个SQL是等价的,但是一个执行N小时都执行不完,一个花了一分钟. 执行计划显示第一个语句是由外面的即将被更新的表驱动内层,相对于是一个NEST LOOP,cost非常大.第二个语句是内层单独执 ...
 - iBatis简单入门教程
			
iBatis 简介: iBatis 是apache 的一个开源项目,一个O/R Mapping 解决方案,iBatis 最大的特点就是小巧,上手很快.如果不需要太多复杂的功能,iBatis 是能够满足 ...
 - Android 2D Graphics学习 Region和Canvas裁剪
			
1.首先介绍Region类 Region,中文意思即区域的意思,它表示的是canvas图层上的某一块封闭的区域. /**构造方法*/ public Region() //创建一个空的区域 publi ...
 - sql rank()函数
			
RANK() OVER([<partiton_by_clause>]) partition_by_clause 将from子句生成的结果集划分为应用到RANK函数的分区. Order_b ...
 - Java网络编程之流——readline()方法的bug
			
readline()方法有一个隐含的bug,它不一定会把一个回车看作行的结束.相反,readline()只识别换行或回车/换行对.当在流中检测到回车时,readline()会在继续之前等待,查看下一个 ...
 - scipy科学计算库
			
特定函数 例贝塞尔函数: 积分 quad,dblquad,tplquad对应单重积分,双重积分,三重积分 from scipy.integrate import quad,dblquad,tplqua ...