2 条题解

  • 0
    @ 2024-12-18 13:58:55

    普通老方法:

    #include <bits/stdc++.h>
    #define INF 0x3f3f3f3f
    #define PI acos(-1.0)
    #define N 100001
    #define MOD 123
    #define E 1e-6
    using namespace std;
    char a[N],b[N],c[N];
    int calculate(int left1,int right1,int left2,int right2)
    {
        if(left1==right1)
        {
            c[left1]=1;
            return c[left1];
        }
        int i;
        for(i=left2;i<=right2;i++)
            if(a[left1]==b[i])
                break;
        if(left2<i)
            c[left1]+=calculate(left1+1,(i-1-left2)+(left1+1),left2,i-1);
        if(i<right2)
            c[left1]+=calculate(right1-(right2-(i+1)),right1,i+1,right2);
        return c[left1];
    }
    int main()
    {
        cin>>a>>b;
        int lena=strlen(a);
        int lenb=strlen(b);
        calculate(0,lena-1,0,lenb-1);
        for(int i=0;i<lena;i++)
        {
            for(int j=1;j<=c[i];j++)
                cout<<a[i];
            cout<<endl;
        }
        return 0;
    }
    
    • 0
      @ 2024-12-18 13:58:33

      结构体非指针方式:

      #include <bits/stdc++.h>
      using namespace std;
      static const int MAXN = 100; // 假定最多100个节点
      struct Node {
          char c;    // 节点字符
          int left;  // 左子节点索引
          int right; // 右子节点索引
      };
      string preOrder, inOrder;
      Node tree[MAXN];
      int nodeCount;
      int charToIndex[256]; // 通过字符快速找到其在 tree 中的下标
      // 根据 preOrder[preL...preR] 和 inOrder[inL...inR] 重建树,返回根节点索引
      int buildTree(int preL, int preR, int inL, int inR) {
          if (preL > preR || inL > inR) return -1;
      
          char rootChar = preOrder[preL];
          // 找到rootChar在中序中的位置
          int rootPos = inL;
          while (rootPos <= inR && inOrder[rootPos] != rootChar) {
              rootPos++;
          }
      
          // 创建根节点
          int rootIndex = charToIndex[(unsigned char)rootChar];
      
          int leftSize = rootPos - inL; // 左子树节点数
          // 递归构建左子树
          tree[rootIndex].left = buildTree(preL + 1, preL + leftSize, inL, rootPos - 1);
          // 递归构建右子树
          tree[rootIndex].right = buildTree(preL + leftSize + 1, preR, rootPos + 1, inR);
      
          return rootIndex;
      }
      
      // 计算节点长度(叶子为1,非叶为左右子树长度和)
      int calcLength(int root) {
          if (root == -1) return 0;
          int leftLen = calcLength(tree[root].left);
          int rightLen = calcLength(tree[root].right);
          if (leftLen == 0 && rightLen == 0) {
              return 1; // 叶子节点长度为1
          }
          return leftLen + rightLen;
      }
      
      // 凹入表示法输出
      void printTree(int root) {
          if (root == -1) return;
          int leftLen = (tree[root].left == -1 ? 0 : calcLength(tree[root].left));
          int rightLen = (tree[root].right == -1 ? 0 : calcLength(tree[root].right));
          int length = (leftLen == 0 && rightLen == 0) ? 1 : leftLen + rightLen;
      
          for (int i = 0; i < length; i++) {
              cout << tree[root].c;
          }
          cout << "\n";
      
          // 输出左子树
          printTree(tree[root].left);
          // 输出右子树
          printTree(tree[root].right);
      }
      
      int main() {
          // 读入先序和中序遍历
          cin >> preOrder >> inOrder;
          // 假设节点数为 preOrder.length() 或 inOrder.length()
          nodeCount = (int)preOrder.size();
      
          // 初始化map
          for (int i = 0; i < 256; i++) {
              charToIndex[i] = -1;
          }
      
          // 根据 preOrder 字符创建节点数组,并记录字符到下标的映射
          for (int i = 0; i < nodeCount; i++) {
              tree[i].c = preOrder[i];
              tree[i].left = -1;
              tree[i].right = -1;
              charToIndex[(unsigned char)preOrder[i]] = i;
          }
      
          // 建树
          int root = buildTree(0, nodeCount - 1, 0, nodeCount - 1);
      
          // 输出
          printTree(root);
      
          return 0;
      }
      
      
      
      • 1

      信息

      ID
      844
      时间
      1000ms
      内存
      256MiB
      难度
      9
      标签
      递交数
      14
      已通过
      4
      上传者