1 条题解

  • 0
    @ 2025-9-9 23:46:53

    C :

    #include<stdio.h>
    #include<string.h>
    char a[10],b[10],s[10];
    int i;
    void f(int n,char *a,char *b,char *s)
    {
        int p;
        if(n<=0)return ;
        p=strchr(a,b[n-1])-a;
        s[i++]=b[n-1];
        f(p,a,b,s);
        f(n-p-1,a+p+1,b+p,s);
    
    }
    int main()
    {
        int n;
        gets(a); gets(b);
        n=strlen(a);
        f(n,a,b,s);
        s[i]=0;
        puts(s);
        return 0;
    }
    
    

    C++ :

    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<ctime>
    #include<cmath>
    #include<string>
    #include<cstdio>
    using namespace std;
    
    char a[15], b[15];
    int v[15], pos;
    
    struct tree{
        char ch;
        struct tree *lson, *rson;
    }*root;
    
    bool right(char ch)
    {
        int i;
        for(i = 0; a[i]; ++i)
        {
            if(a[i] == ch) {
                v[i] = true; break;
            }
        }
        i++;
        if(!v[i] && a[i] != '\0') return true;
        return false;
    }
    
    bool left(char ch)
    {
        int i;
        for(i = 0; a[i]; ++i)
        {
            if(a[i] == ch) {
                v[i] = true; break;
            }
        }
        i--;
        if(i >= 0 && !v[i]) return true;
        return false;
    }
    
    void build(struct tree *rt)
    {
        //cout<<pos<<endl;
        rt->ch = b[pos--];
        rt->lson = new tree;
        rt->rson = new tree;
        if( right(rt->ch) )
            build(rt->rson);
        else rt->rson = NULL;
        if( left(rt->ch) )
            build(rt->lson);
        else rt->lson = NULL;
    }
    
    void travel(struct tree *rt)
    {
        if(rt == NULL) return;
        putchar(rt->ch);
        travel(rt->lson);
        travel(rt->rson);
    }
    
    int main()
    {
        cin>>a>>b;
        memset(v, false, sizeof(v));
        root = new tree;
        pos = strlen(b) - 1;
        build(root);
        travel(root); puts("");
        return 0;
    }
    
    
    • 1

    信息

    ID
    1072
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者