天天看點

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 ;
}