/*
    第2種情報処理技術者試験    平成12年度・秋期・午後 問7
                                BohYoh Shibata PREPARATION
*/

#include <string.h>
#include <stdio.h>

typedef struct {
    char flag;        /* フラグ */
    char *name;       /* 氏名へのポインタ */
    int  nlen;        /* 氏名の長さ */
    char *addr;       /* メールアドレスへのポインタ */
    int  alen;        /* メールアドレスの長さ */
    char *time;       /* 更新日時へのポインタ */
} ADDR;

int namecmp(char *, int, char *, int);

void addrsync(ADDR desk[], ADDR note[], ADDR sync[])
{
    int didx=0, nidx=0, sidx=0, comp;

    while ( desk[didx].flag != 'E' || note[nidx].flag != 'E' ) {
        comp = namecmp(desk[didx].name, desk[didx].nlen,
                            note[nidx].name, note[nidx].nlen);
        if (comp < 0) {
            if (desk[didx].flag != 'D') {
                sync[sidx] = desk[didx];
                sync[sidx++].flag = ' ';
            }
            didx++ ;
        }
        else if (comp > 0) {
            if (note[nidx].flag != 'D') {
                sync[sidx] = note[nidx];
                sync[sidx++].flag = ' ';
            }
            nidx++ ;
        }
        else {
            if (strncmp(desk[didx].time, note[nidx].time, 19)
                    > 0) {
                if (desk[didx].flag != 'D') {
                    sync[sidx] = desk[didx];
                    sync[sidx++].flag = ' ';
                }
            }
            else {
                if (note[nidx].flag != 'D') {
                    sync[sidx] = note[nidx];
                    sync[sidx++].flag = ' ';
                }
            }
            didx++, nidx++ ;
        }
    }
    sync[sidx].flag = 'E';
    sync[sidx].nlen = -1;
    sync[sidx].alen = -1;
}

int namecmp(char *str1, int len1, char *str2, int len2)
{
    int leng, result;

    if (len1 == -1) return 1;
    if (len2 == -1) return -1;
    leng = (len1 < len2) ? len1 : len2;
    result = strncmp(str1, str2, leng);
    if (result == 0)
        result = len1 - len2 ;
    return result;
}