Help me with this problem please


  • 0
    E

    bool trans(char *a, char *b, int l)
    {
    bool diff = false;
    for (int i = 0; i < l; i++)
    {
    if (a[i] != b[i])
    {
    if (diff)
    {
    return false;
    }
    diff = true;
    }
    }
    return true;
    }

    bool equal(char *a, char *b, int l)
    {
    for (int i = 0; i < l; i++)
    {
    if (a[i] != b[i])
    {
    return false;
    }
    }
    return true;
    }

    typedef struct Info
    {
    int *parent;
    int *child;
    int parent_layer;
    int child_layer;
    int parent_num;
    int child_num;
    int parent_path;
    int child_path;
    }Info;
    void init_info(Info *in)
    {
    in->parent_layer = in->child_layer = -1;
    in->parent_num = in->child_num = 0;
    in->parent_path = in->child_path = 0;
    in->parent = in->child = NULL;
    }

    void init_info_parent(Info in, int l)
    {
    in->parent = (int
    )malloc(l*sizeof(int));
    }

    void init_info_child(Info in, int l)
    {
    in->child = (int
    )malloc(l*sizeof(int));
    }

    char*** findLadders(char* beginWord, char* endWord, char** wordList, int wordListSize, int** columnSizes, int* returnSize)
    {
    int word_l = sizeof(beginWord)-1;
    Info infos = (Info)malloc(wordListSize*sizeof(Info));
    for (int i = 0; i < wordListSize; i++)
    {
    init_info(&infos[i]);
    }

    int *p_list1 = (int*)malloc(wordListSize*sizeof(int));
    int *c_list1 = (int*)malloc(wordListSize*sizeof(int));
    int *p_list2 = (int*)malloc(wordListSize*sizeof(int));
    int *c_list2 = (int*)malloc(wordListSize*sizeof(int));
    int p_num = 0, c_num = 0;
    int *p_list = NULL, *p_next = NULL, *c_list = NULL, *c_next = NULL;
    
    int solution_num = 0;
    int trans_l = 0;
    
    p_num = 0;
    for (int i = 0; i < wordListSize; i++)
    {
    	if (trans(beginWord, wordList[i], word_l))
    	{
    		if (!equal(beginWord, wordList[i], word_l))
    		{
    			infos[i].parent_layer = 0;
    			infos[i].parent_path = 1;
    			p_list1[p_num] = i;
    			p_num++;
    		}
    	}
    }
    
    c_num = 0;
    int rear = -1;
    for (int i = 0; i < wordListSize; i++)
    {
    	if (trans(endWord, wordList[i], word_l))
    	{
    		if (!equal(endWord, wordList[i], word_l))
    		{
    			infos[i].child_layer = 1;
    			infos[i].child_path = 1;
    			c_list1[c_num] = i;
    			c_num++;
    		}
    		else
    		{
    			infos[i].child_layer = 0;
    			infos[i].child_path = 1;
    			rear = i;
    		}
    	}
    }
    
    if (c_num == 0 || p_num == 0 || rear == -1)
    {
    	c_num = p_num = 0;
    }
    
    if (rear > -1)
    {
    	for (int i = 0; i < c_num; i++)
    	{
    		init_info_child(&infos[c_list1[i]], wordListSize);
    		infos[c_list1[i]].child_num = 1;
    		infos[c_list1[i]].child[0] = rear;
    	}
    
    	int temp_num;
    	bool bingo = false;
    	p_list = p_list1;
    	p_next = p_list2;
    	c_list = c_list1;
    	c_next = c_list2;
    	while (c_num>0 && p_num > 0)
    	{
    		temp_num = 0;
    		for (int i = 0; i < p_num; i++)
    		{
    			for (int j = 0; j < wordListSize; j++)
    			{
    				if (trans(wordList[p_list[i]], wordList[j], word_l) && j != p_list[i])
    				{
    					if (infos[j].parent_layer == -1)
    					{
    						infos[j].parent_layer = infos[p_list[i]].parent_layer + 1;
    					}
    					if (infos[p_list[i]].parent_layer < infos[j].parent_layer)
    					{
    						if (infos[j].parent_num == 0)
    						{
    							init_info_parent(&infos[j], wordListSize);
    						}
    						infos[j].parent[infos[j].parent_num] = p_list[i];
    						infos[j].parent_num++;
    						infos[j].parent_path += infos[p_list[i]].parent_path;
    
    						p_next[temp_num] = j;
    						temp_num++;
    
    						if (infos[j].child_num > 0)
    						{
    							bingo = true;
    						}
    					}
    				}
    			}
    		}
    
    		if (bingo)
    		{
    			c_num = 0;
    			p_num = temp_num;
    			break;
    		}
    
    		if (p_list == p_list1)
    		{
    			p_list = p_list2;
    			p_next = p_list1;
    		}
    		else
    		{
    			p_list = p_list1;
    			p_next = p_list2;
    		}
    		p_num = temp_num;
    
    
    		temp_num = 0;
    		for (int i = 0; i < c_num; i++)
    		{
    			for (int j = 0; j < wordListSize; j++)
    			{
    				if (trans(wordList[c_list[i]], wordList[j], word_l) && j != c_list[i])
    				{
    					if (infos[j].child_layer == -1)
    					{
    						infos[j].child_layer = infos[c_list[i]].child_layer + 1;
    					}
    					if (infos[c_list[i]].child_layer < infos[j].child_layer)
    					{
    						if (infos[j].child_num == 0)
    						{
    							init_info_child(&infos[j], wordListSize);
    						}
    						infos[j].child[infos[j].child_num] = c_list[i];
    						infos[j].child_num++;
    						infos[j].child_path += infos[c_list[i]].child_path;
    
    						c_next[temp_num] = j;
    						temp_num++;
    
    						if (infos[j].parent_num > 0)
    						{
    							bingo = true;
    						}
    					}
    				}
    			}
    		}
    
    		if (bingo)
    		{
    			p_num = 0;
    			c_num = temp_num;
    			break;
    		}
    
    		if (c_list == c_list1)
    		{
    			c_list = c_list2;
    			c_next = c_list1;
    		}
    		else
    		{
    			c_list = c_list1;
    			c_next = c_list2;
    		}
    		c_num = temp_num;
    	}
    
    	solution_num = 0;
    	for (int i = 0; i < p_num; i++)
    	{
    		solution_num += infos[p_next[i]].parent_path*infos[p_next[i]].child_path;
    		trans_l = infos[p_next[i]].parent_layer + infos[p_next[i]].child_layer + 2;
    	}
    	for (int i = 0; i < c_num; i++)
    	{
    		solution_num += infos[c_next[i]].parent_path*infos[c_next[i]].child_path;
    		trans_l = infos[c_next[i]].parent_layer + infos[c_next[i]].child_layer + 2;
    	}
    }
    
    char ***res = NULL;
    *columnSizes = NULL;
    res = (char***)malloc(sizeof(char**)*solution_num);
    *columnSizes = (int*)malloc(solution_num*sizeof(int));
    *returnSize = solution_num;
    for (int i = 0; i < solution_num; i++)
    {
    	res[i] = NULL;
    	res[i] = (char**)malloc(sizeof(char*)*trans_l);
    	(*columnSizes)[i] = trans_l;
    	for (int j = 0; j < trans_l; j++)
    	{
    		res[i][j] = NULL;
    		res[i][j] = (char*)malloc(word_l + 1);
    		memset(res[i][j], 0, word_l + 1);
    	}
    }
    
    
    if (c_num == 0)
    {
    	c_num = p_num;
    	for (int i = 0; i < p_num; i++)
    	{
    		c_next[i] = p_next[i];
    	}
    }
    
    int index = 0;
    int parent_l = 0, child_l = 0;
    int temp_num1 = 0, temp_num2 = 0;
    for (int i = 0; i < c_num; i++)
    {
    	for (int j = 0; j < infos[c_next[i]].child_path*infos[c_next[i]].parent_path; j++)
    	{
    		memcpy(res[index][infos[c_next[i]].parent_layer + 1], wordList[c_next[i]], word_l);
    		index++;
    	}
    	parent_l = infos[c_next[i]].parent_layer;
    	child_l = infos[c_next[i]].child_layer;
    
    	for (int j = 0; j < infos[c_next[i]].parent_num; j++)
    	{
    		if (infos[infos[c_next[i]].parent[j]].child_path == 0)
    		{
    			infos[infos[c_next[i]].parent[j]].child_path = infos[c_next[i]].child_path;
    			p_list[temp_num1] = infos[c_next[i]].parent[j];
    			temp_num1++;
    		}
    		else
    		{
    			infos[infos[c_next[i]].parent[j]].child_path += infos[c_next[i]].child_path;
    		}
    	}
    
    	for (int j = 0; j < infos[c_next[i]].child_num; j++)
    	{
    		if (infos[infos[c_next[i]].child[j]].parent_path == 0)
    		{
    			infos[infos[c_next[i]].child[j]].parent_path = infos[c_next[i]].parent_path;
    			c_list[temp_num2] = infos[c_next[i]].child[j];
    			temp_num2++;
    		}
    		else
    		{
    			infos[infos[c_next[i]].child[j]].parent_path += infos[c_next[i]].parent_path;
    		}
    	}
    }
    p_num = temp_num1;
    c_num = temp_num2;
    
    for (int l = 0; l < parent_l; l++)
    {
    	temp_num1 = 0;
    	index = 0;
    	for (int i = 0; i < p_num; i++)
    	{
    		for (int j = 0; j < infos[p_list[i]].child_path*infos[p_list[i]].parent_path; j++)
    		{
    			memcpy(res[index][infos[p_list[i]].parent_layer + 1], wordList[p_list[i]], word_l);
    			index++;
    		}
    
    		for (int j = 0; j < infos[p_list[i]].parent_num; j++)
    		{
    			if (infos[infos[p_list[i]].parent[j]].child_path == 0)
    			{
    				infos[infos[p_list[i]].parent[j]].child_path = infos[p_list[i]].child_path;
    				p_next[temp_num1] = infos[p_list[i]].parent[j];
    				temp_num1++;
    			}
    			else
    			{
    				infos[infos[p_list[i]].parent[j]].child_path += infos[p_list[i]].child_path;
    			}
    		}
    	}
    
    	if (p_list == p_list1)
    	{
    		p_list = p_list2;
    		p_next = p_list1;
    	}
    	else
    	{
    		p_list = p_list1;
    		p_next = p_list2;
    	}
    	p_num = temp_num1;
    }
    
    for (int l = 0; l < child_l; l++)
    {
    	temp_num1 = 0;
    	index = 0;
    	for (int i = 0; i < c_num; i++)
    	{
    		for (int j = 0; j < infos[c_list[i]].child_path*infos[c_list[i]].parent_path; j++)
    		{
    			memcpy(res[index][trans_l - 1 - infos[c_list[i]].child_layer], wordList[c_list[i]], word_l);
    			index++;
    		}
    
    		for (int j = 0; j < infos[c_list[i]].child_num; j++)
    		{
    			if (infos[infos[c_list[i]].child[j]].parent_path == 0)
    			{
    				infos[infos[c_list[i]].child[j]].parent_path = infos[c_list[i]].parent_path;
    				c_next[temp_num1] = infos[c_list[i]].child[j];
    				temp_num1++;
    			}
    			else
    			{
    				infos[infos[c_list[i]].child[j]].parent_path += infos[c_list[i]].parent_path;
    			}
    		}
    	}
    
    	if (c_list == c_list1)
    	{
    		c_list = c_list2;
    		c_next = c_list1;
    	}
    	else
    	{
    		c_list = c_list1;
    		c_next = c_list2;
    	}
    	c_num = temp_num1;
    }
    
    for (int i = 0; i < solution_num; i++)
    {
    	memcpy(res[i][0], beginWord, word_l);
    }
    
    for (int i = 0; i < wordListSize; i++)
    {
    	if (infos[i].child_num>0)
    	{
    		free(infos[i].child);
    	}
    	if (infos[i].parent_num>0)
    	{
    		free(infos[i].parent);
    	}
    }
    free(infos);
    free(p_list1);
    free(c_list1);
    free(p_list2);
    free(c_list2);
    return res;
    

    }

    This is my code. It goes right when I press 'Run Code', but when I Submit, there is a error.
    Runtime Error Message:
    Line 297: load of misaligned address 0x000000000031 for type 'char *', which requires 8 byte alignment

    why does this happen?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.