1 条题解

  • 0
    @ 2025-2-14 21:26:47

    C :

    #include <stdio.h>
    struct Node
    {
    struct Node *lchild;
    struct Node *rchild;
    char c;
    }Tree[50];                                                            
    int loc;
    struct Node *creat()
    {
    	Tree[loc].lchild=Tree[loc].rchild=NULL;
    	return &Tree[loc++];
    }
    char str1[50],str2[50];
    int Height (struct Node *T)  
    {    
        if(T==NULL) return 0;  
        else  
        {  
            int m = Height ( T->lchild );  
            int n = Height(T->rchild);  
            return (m > n) ? (m+1) : (n+1);   
        }  
    }  
      
    struct Node *build(int s1,int e1,int s2,int e2)
    {
    struct Node* ret=creat();
    ret->c=str1[s1];
    int i,x;
    for(i=s2;i<=e2;i++)
    {
    	if(str2[i]==str1[s1]){x=i;break;}
    }
    if(x!=s2) ret->lchild=build(s1+1,s1+(x-s2),s2,x-1);
    if(x!=e2) ret->rchild=build(s1+(x-s2)+1,e1,x+1,e2);
    return ret;
    }
    int main()
    {
    int n;
    while(scanf("%d %s",&n,str1)!=EOF)
    {
    scanf("%s",str2);
    loc=0;
    struct Node *T=build(0,n-1,0,n-1);
    printf("%d\n",Height(T));
    }
    return 0;
    }
    

    C++ :

    #include<stdio.h>
    #include <iostream>
    #include <string.h>
    
    using namespace std;
    
    char pre[100];
    char mid[100];
    
    struct Node
    {
    	char value;
    	struct Node *left;
    	struct Node *right;
    }*root;
    
    Node* build_tree(int p_s, int p_e, int i_s, int i_e)
    {
    	if (p_e < p_s) return NULL;
    	int i = 0;
    	Node *tmp = new Node();
    	for (i = i_s; i <= i_e; i++)
    	{
    		if (pre[p_s] == mid[i])
    			break;
    	}
    	tmp->value = pre[p_s];
    
    	tmp->left = build_tree(p_s + 1, p_s + i - i_s, i_s, i - 1);
    	tmp->right = build_tree(p_s + i - i_s + 1, p_e, i + 1, i_e);
    
    	return tmp;
    }
    
    int ans;
    
    void DFS(Node *root, int lev)
    {
    	if (lev > ans) ans = lev;
    	if (root->left != NULL)
    		DFS(root->left, lev + 1);
    
    	if (root->right != NULL)
    		DFS(root->right, lev + 1);
    }
    
    
    
    int main()
    {
    	int n;
    	while (scanf("%d", &n) != EOF)
    	{
    		scanf("%s%s", pre, mid);
    
    		root = build_tree(0, n - 1, 0, n - 1);
    		ans = 0;
    		DFS(root, 0);
    		//printf("sda\n");
    
    		printf("%d\n", ans + 1);
    	}
    	return 0;
    }
    
    

    Java :

    import java.util.Scanner;
    
    public class Main {
    
    	static class Node{
    
    		Node left;
    		Node right;
    		char value;
    	}
    	
    	static Node node[];
    	static int N;
    	
    	
    
    	static Node build_tree(String pre,String mid,Integer num){
    		
    		
    		Node node=new Node();
    
    		if(num==0) return null; 
    			
    		node.value=pre.charAt(0);
    		
    		int i;
    		
    		for (i = 0; i < num; i++) {
    			if(mid.charAt(i)==pre.charAt(0))
    				break;
    		}
    		
    		
    		node.left=build_tree(pre.substring(1,i+1),mid.substring(0,i),i);
    		node.right=build_tree(pre.substring(i+1,pre.length()),mid.substring(i+1,mid.length()),num-i-1);
    	
    	    return node;
    	}
    	
    	
    	public static int getHight(Node node){
    		
    		int RHight=0,LHight=0;
    		int THeight;
    		
    	    if(node==null) 
    	        THeight=0; 
             else {
         		
         		LHight=getHight(node.left);
         		RHight=getHight(node.right);
         		
         		THeight=LHight>RHight? LHight:RHight;
         	    THeight++; 
    		}
    
        
            return THeight;
    
    	}
    	
    	
    	public static void main(String[] args) {
    		
            Scanner cin=new Scanner(System.in);
            while(cin.hasNext()){
            	
               N=cin.nextInt();
               String pre=cin.next();
               String mid=cin.next();
            
               node=new Node[N];
            
    		   Node node=build_tree(pre, mid, N);
    		   System.out.println(getHight(node));
            }
    		
    	}
    
    }
    
    
    • 1

    信息

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