#include <stdio.h>
#include <stdlib.h>
typedef int bool;
#define true 1
#define false 0
typedef struct node
{
int coef, exp;
struct node *next;
} NODE;
NODE * multiplication(NODE *, NODE *);
void input(NODE *);
void output(NODE *);
NODE* Link(NODE* p,int i) //?????,???i???,i?0??
{
int j;
for (j = 0; j <= i; j++)
{
p = p->next;
if (p == NULL) return NULL;
}
return p;
}
int Length(NODE* p) //?????,????,
{
int j=0;
while (p->next != NULL)
{
j++;
p = p->next;
}
return j;
}
void Append(NODE* p, int coef0, int exp0) //???????????????????,
{
p->next = (NODE*)malloc(sizeof(NODE));
p = p->next;
p->coef = coef0;
p->exp = exp0;
p->next=NULL;
}
void DeleteLinklist(NODE* head) //????
{
NODE *pPre;
while (head != NULL)
{
pPre = head;
head = head->next;
free(pPre);
}
}
NODE * multiplication(NODE *p1, NODE *p2) //????
{
NODE* ans,*p3;
NODE* Temp1,* Temp2;
int i,j;
p3 = (NODE*)malloc(sizeof(NODE)); //????p3
ans = p3; //ans??p3????,????
int len = Length(p1) + Length(p2)-1; //p3?????=?????????
for (i = 0; i < len; i++) //????p3????
{
Append(p3, 0, i);
p3=p3->next;
for (j = 0; j <= i; j++)
{
Temp1 = Link(p1,j);
Temp2 = Link(p2, i - j);
if ((Temp1!=NULL)&&(Temp2!=NULL)) p3->coef += (Temp1->coef*Temp2->coef);
}
}
return ans;
}
void input(NODE * p) //?????,?????????
{
int a, b;
int i = 0;
rewind(stdin);
while(scanf("<%d,%d>,", &a, &b)) //????,????????a?b?
{
for (i; i < b; i++)
{
Append(p, 0, i); //????,????=b
p=p->next;
}
Append(p, a, b); //??
p=p->next;
i++;
}
}
void output(NODE * head)
{
bool b = false; //???????????
while (head->next != NULL)
{
head = head->next;
if (head->coef != 0)
{
b = true; //??????????0,????????
printf("<%d,%d>,", head->coef, head->exp);
}
}
if (!b) printf("<0,0>,"); //??????????,?????0???,??<0,0>
printf("\n");
}
int main()
{
int i;
NODE * head1, *head2, *head3;
head1 = (NODE *)malloc(sizeof(NODE));
input(head1);
head2 = (NODE *)malloc(sizeof(NODE));
input(head2);
head3 = multiplication(head1, head2);
output(head3);
DeleteLinklist(head1);
DeleteLinklist(head2);
DeleteLinklist(head3);
return 0;
}