天天看点

B - No Name UVALive - 6319 思维or链表

题意:

t组数据,每组数据给定一组字符串

有两种操作

I ac 3

P 0 3

I + 字符串 +位置

把该字符串插到母串的指定位置

P l r

输出字符串从l 到 r

题目链接:

https://cn.vjudge.net/contest/183985#problem/B

比赛的时候用链表做的

Xuhuang大佬用一种巧妙的思维解决了这道题,

实际直接暴力也可以,时限很足

下面讲解一下Xuhuang的思路

要把b串插入到a串

首先把a串中p位置之后的

字符分割开,插入到b串后面

之后再把整个b串插入到a串后面

注意题目示例有点坑,不要直接复制PDF上的,上面的最后P 0 11不对

ac code:

#include <iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<vector>
#include<cstdio>
#include<list>
#include<cstring>
using namespace std;
list<char>o;
char a[],b[],c[],d[];
int main()
{
    int t;
    cin>>t;

    while(t--)
    {

        scanf("%s",a);
        while()
        {
            scanf("%s",c);
            if(c[]=='E')
            {
                break;
            }
            if(c[]=='I')
            {
                int p;
                scanf("%s%d",b,&p);
                strcat(b,a+p);
                a[p] = ; //  '/0' 等于0
                strcat(a,b);
          //      printf("%s\n",a);
            }
            else
            {
                int j = ;
                int l,r;
                scanf("%d%d",&l,&r);

                for(int i=l;i<=r;i++)
                {
                    d[j] = a[i];
                    j++;

                }
                //复制给d,提高输出速度

                d[j] = ;
                printf("%s\n",d);
            }
        }

    }
    return ;
}
           

链表做的:

#include <iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<vector>
#include<cstdio>
#include<list>
#include<cstring>
using namespace std;
list<char>o;
int main()
{
    int t;
    cin>>t;

    char aa[];
    while(t--)
    {
        o.clear();
        list<char>temp;
        scanf("%s",aa);

        list<char>::iterator it,its;
        int len = strlen(aa);
        for(int i=; i<len; i++)
        {
            temp.push_back(aa[i]);
        }
        o.insert(o.begin(),temp.begin(),temp.end());

//        for(it = o.begin(); it!=o.end(); it++)
//        {
//            printf("%c",*it);
//        }
//        printf("\n");

        while()
        {
            char order[];
            scanf("%s",order);

            if(order[]=='E')
            {
                break;
            }
            else if(order[]=='P')
            {
                int ss,ee,ii = ;
                scanf("%d %d",&ss,&ee);
                for(it = o.begin();; it++)
                {
                    ii++;
                    if(ii>ss)
                    {
                        break;
                    }
                }
                for(;; it++)
                {
                    ss++;
                    printf("%c",*it);
                    if(ss>ee)
                    {
                        break;
                    }

                }
                printf("\n");
            }
            else
            {
                scanf("%s",aa);
                temp.clear();
                int pp;
                scanf("%d",&pp);
                int len = strlen(aa);
                for(int i=; i<len; i++)
                {
                    temp.push_back(aa[i]);
                }

                for(it =o.begin();; it++)
                {

                    if(pp==)
                        break;
                    pp--;
                }
                o.insert(it,temp.begin(),temp.end());

//                for(its = o.begin(); its!=o.end(); its++)
//                {
//                    printf("%c",*its);
//                }
//                printf("\n");

            }
        }


    }
    return ;
}