ytu 2228 判断链表

来源:互联网 发布:windows邮件客户端 编辑:程序博客网 时间:2024/06/11 21:12

题目链接:http://202.194.119.110/problem.php?id=2228


代码:

#include<stdio.h>#include<malloc.h>typedef struct node node;struct node{    int num;    node *next;};int main(){    int m,n;    node *list1 = (node*)malloc(sizeof(node));    node *list2 = (node*)malloc(sizeof(node));    list1->next = NULL;    list2->next = NULL;    int count = 0;    int fubenn = 0;;    node *current = NULL;    scanf("%d",&m);    while(m--)//创建较长的链表    {        current = (node*)malloc(sizeof(node));        scanf("%d",¤t->num);        current->next = list1->next;        list1->next = current;    }    scanf("%d",&n);    fubenn = n;    while(n--)//创建较短的链表    {        current = (node*)malloc(sizeof(node));        scanf("%d",¤t->num);        current->next = list2->next;        list2->next = current;    }    node *temp = NULL;    node *fal = NULL;    node *fuben = list2;    while(list2->next)//枚举,判断短序列是否是长序列的子序列    {        temp = list1;        while(temp->next)        {            if(temp->next->num == list2->next->num)            {                count++;                fal = temp->next;//扫描到的节点就释放掉                temp->next = fal->next;                free(fal);                break;            }            temp->next = temp->next->next;        }        current = list2->next;//扫描过的节点就释放掉        list2->next = current->next;        free(current);    }    free(fuben);    current = list1->next;    free(list1);    while(current)//销毁链表,释放剩余的空间    {        list1 = current;        current = current->next;        free(list1);    }    current = list1 = list2 = fuben = fal = temp = NULL;    if(count == fubenn)        printf("1\n");    else        printf("0\n");    return 0;}

智力有限,想不出太好的方法。虽然ac了,不过在 判断短序列是否是长序列的子序列的那个循环 内还是有些问题。水平有限,只能发现那里有问题,如果哪位大神发现了具体的问题,求告知。其实感觉我的逻辑也是有些问题。







0 0