zoukankan      html  css  js  c++  java
  • POJ2887(块状链表)

    Big String
    Time Limit: 1000MS   Memory Limit: 131072K
    Total Submissions: 6346   Accepted: 1525

    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:

    1. 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.
    2. 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
    标准块状链表.
    #include"cstdio"
    #include"cstring"
    #include"cmath"
    #include"algorithm"
    using namespace std;
    const int MAXN=1005;
    char str[MAXN*MAXN];
    struct Block_List{
        int size,next;
        char s[2*MAXN];
        Block_List()
        {
            memset(s,0,sizeof(s));
            size=0;
            next=-1;
        }
        
        void push(char ch)
        {
            s[size++]=ch;
        }
        
        void insert(int pos,char ch)
        {
            for(int i=size;i>pos;i--)
            {
                s[i]=s[i-1];
            }
            s[pos]=ch;
            size++;    
        }
    }bkl[MAXN];
    
    int bsize,cnt,m;
    void Init()
    {
        gets(str);
        scanf("%d",&m);
        bsize=(int)sqrt(double(strlen(str)+m));
        for(int i=0;str[i];i++)
        {
            if(bkl[cnt].size==bsize)
            {
                bkl[cnt].next=cnt+1;
                cnt++;
            }
            bkl[cnt].push(str[i]);
        }
        cnt++;
    }
    
    void Update(int u)
    {
        if(bkl[u].size<2*bsize)    return ;
        /*
        for(int i=bsize;i<bkl[u].size;i++)
        {
            bkl[cnt].push(bkl[u].s[i]);
        }
        */
        strcpy(bkl[cnt].s,bkl[u].s+bsize);
        bkl[cnt].size=strlen(bkl[u].s)-bsize;
        bkl[u].size=bsize;
        bkl[cnt].next=bkl[u].next;
        bkl[u].next=cnt;
        cnt++;
    }
    void handle()
    {
        int cas=0;
        while(++cas<=m)
        {
            char op[5];
            scanf("%s",op);
            if(op[0]=='Q')
            {
                int pos;
                scanf("%d",&pos);
                int i;
                for(i=0;pos>bkl[i].size;i=bkl[i].next)
                {
                    pos-=bkl[i].size;
                }
                printf("%c
    ",bkl[i].s[pos-1]);
            }
            else
            {
                char ch[5];
                int pos;
                scanf("%s%d",ch,&pos);
                int i;
                for(i=0;pos>bkl[i].size&&bkl[i].next!=-1;i=bkl[i].next)
                {
                    pos-=bkl[i].size;
                }
                bkl[i].insert(pos-1,ch[0]);
                Update(i);
            }
        }
    }
    int main()
    {
        Init();
        handle();    
        return 0;
    }
  • 相关阅读:
    servlet规范
    Java --Servlet 32个经典问题
    TCP的三次握手与四次挥手理解及面试题(很全面)
    TCP‘三次握手’和‘四次挥手’(通俗易懂)
    leetcode:122. Best Time to Buy and Sell Stock II(java)解答
    STM32通过调用库函数进行编程
    Swift下调用Touch ID实现指纹识别
    SpringMVC+MyBatis+JMS+JTA(分布式事务)
    windows下的两个等待函数
    Ubuntu 14.04正式公布,一个不眠之夜
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5142187.html
Copyright © 2011-2022 走看看