#include <stdio.h>
#include <stdlib.h>
typedef struct LNode
{
int data;
int index;
struct LNode* next;
}LNode;
void initLinkList(LNode* pL)
{
int m, n;
LNode* pR, * pS;
pR = pL;
while (1)
{
scanf("%d,%d", &m, &n);
if (m == 0 && n == 0)
break;
pS = (LNode*)malloc(sizeof(LNode));
pS->next = NULL;
pS->data = m;
pS->index = n;
pR->next = pS;
pR = pS;
}
}
void opLinkList(LNode* pLA, LNode* pLB)
{
LNode* pR1, * pR, * pS;
int m, n;
while (1)
{
pR1 = pLA;
pR = pLA->next;
scanf("%d,%d", &m, &n);
if (m == 0 && n == 0)
break;
while (pR != NULL && n > pR->index)
{
pR1 = pR1->next;
pR = pR->next;
}
if (pR == NULL || n < pR->index)
{
pS = (LNode*)malloc(sizeof(LNode));
pS->data = m;
pS->index = n;
pR1->next = pS;
pS->next = pR;
}
if (n == pR->index)
{
pR->data += m;
}
}
pR1 = pLA;
pR = pLA->next;
while (pR != NULL)
{
if (pR->data == 0)
{
pR1->next = pR->next;
pR = pR->next;
}
else
{
pR1 = pR1->next;
pR = pR->next;
}
}
}
void dispLinkList(LNode* pL)
{
LNode* pR;
pR = pL->next;
while (pR != 0)
{
printf("%dx^%d", pR->data, pR->index);
pR = pR->next;
if (pR != NULL)
printf("+");
}
}
int main()
{
LNode* pLA, * pLB;
pLA = (LNode*)malloc(sizeof(LNode));
pLA->next = NULL;
pLB = (LNode*)malloc(sizeof(LNode));
pLB->next = NULL;
initLinkList(pLA);
opLinkList(pLA, pLB);
dispLinkList(pLA);
return 0;
}