IDENTIFICATION                   DIVISION.
PROGRAM-ID.                      ABC_098_B.
ENVIRONMENT                      DIVISION.
INPUT-OUTPUT                     SECTION.
FILE-CONTROL.
    SELECT X1   ASSIGN TO "X1.DAT"
        FILE STATUS IS X1-STATUS.
    SELECT Y1   ASSIGN TO "Y1.DAT"
        FILE STATUS IS Y1-STATUS.
    SELECT XS1  ASSIGN TO "XS1.WRK".
    SELECT YS1  ASSIGN TO "YS1.WRK".
DATA                             DIVISION.
FILE                             SECTION.
    FD X1.
        01 X11.
            03 X PIC X(1).

    FD Y1.
        01 Y11.
            03 Y PIC X(1).

    SD XS1.
        01 XS11.
            03 XS PIC X(1).

    SD YS1.
        01 YS11.
            03 YS PIC X(1).

WORKING-STORAGE                  SECTION.
    01 N      PIC 9(3).
    01 S      PIC X(101).

    01 chr    PIC X(1)   VALUE SPACE.
    01 str    PIC X(101) VALUE SPACE.

    01 i      PIC 9(3).
    01 j      PIC 9(3).

    01 k      PIC 9(3).
    01 l      PIC 9(3).

    01 yidx   PIC 9(3).

    01 cur    PIC 9(3).

    01 len    PIC 9(1)   VALUE 1.
    01 cnt    PIC 9(2)   VALUE 0.
    01 cntmax PIC 9(2)   VALUE 0.

    01 xcnt   PIC 9(2)   VALUE 0.
    01 ycnt   PIC 9(2)   VALUE 0.

    01 XNUM   PIC 9(2)   VALUE 0.
    01 YNUM   PIC 9(2)   VALUE 0.

    01 wkx    PIC X(1).
    01 wky    PIC X(1).
    01 ans    PIC Z9.

    01 FLAGS.
        03 X1-EMPTY PIC X  VALUE "N".
            88 NO-MORE-X1  VALUE "Y".
        03 Y1-EMPTY PIC X  VALUE "N".
            88 NO-MORE-Y1  VALUE "Y".

    01 XSTATUS.
        03 X1-STATUS PIC XX.

    01 YSTATUS.
        03 Y1-STATUS PIC XX.

PROCEDURE                        DIVISION.
MAIN.
    ACCEPT N.
    ACCEPT S.

    OPEN OUTPUT X1.
    CLOSE X1.

    MOVE SPACE TO wkx.
    MOVE SPACE TO wky.

    PERFORM VARYING i FROM 1 BY 1 UNTIL N < i
        MOVE S(i : len) TO chr

        OPEN EXTEND X1
        MOVE chr TO X
        ADD 1 TO XNUM
        WRITE X11
        CLOSE X1

        MOVE SPACE TO wkx
        MOVE 0 TO xcnt

        IF X1-STATUS = "00" THEN
            SORT XS1
                ASCENDING KEY XS
                WITH DUPLICATES IN ORDER
                    USING X1
                    OUTPUT PROCEDURE IS CREATE-X1LIST
        END-IF

        OPEN OUTPUT Y1
        CLOSE Y1

        COMPUTE cur = i + 1

        MOVE S(cur : N - i) TO str

        OPEN OUTPUT Y1

        PERFORM VARYING yidx FROM 1 BY 1 UNTIL N - 1 < yidx
            MOVE str(yidx : len) TO chr
            WRITE Y11 FROM chr
        END-PERFORM

        CLOSE Y1

        SUBTRACT i FROM N GIVING YNUM
        MOVE SPACE TO wky

        IF "00" = Y1-STATUS THEN
            SORT YS1
                ASCENDING KEY YS
                WITH DUPLICATES IN ORDER
                    USING Y1
                    OUTPUT PROCEDURE IS CREATE-Y1LIST
        END-IF

        MOVE xcnt TO XNUM
        MOVE ycnt TO YNUM
        MOVE 0 TO cnt

        OPEN INPUT X1

        IF "00" = X1-STATUS THEN
            MOVE "N" TO X1-EMPTY
            PERFORM UNTIL NO-MORE-X1
                READ X1
                    AT END
                        MOVE "Y" TO X1-EMPTY
                    NOT AT END

                        OPEN INPUT Y1

                        IF "00" = Y1-STATUS THEN
                            MOVE "N" TO Y1-EMPTY
                            PERFORM UNTIL NO-MORE-Y1
                                READ Y1
                                    AT END
                                        MOVE "Y" TO Y1-EMPTY
                                    NOT AT END
                                        IF X = Y THEN
                                            ADD 1 TO cnt
                                        END-IF
                            END-PERFORM
                        END-IF

                        CLOSE Y1

            END-PERFORM
        END-IF

        CLOSE X1

        COMPUTE cntmax = FUNCTION MAX(cntmax, cnt)

    END-PERFORM.

    MOVE cntmax TO ans.

    DISPLAY ans.

    OPEN OUTPUT X1.
    CLOSE X1.

    OPEN OUTPUT Y1.
    CLOSE Y1.

    STOP RUN.

CREATE-X1LIST SECTION.
    OPEN OUTPUT X1
    CLOSE X1

    OPEN EXTEND X1

    MOVE "NO" TO X1-EMPTY
    PERFORM UPDATE-X1LIST
        UNTIL NO-MORE-X1

    CLOSE X1.

UPDATE-X1LIST SECTION.
    RETURN XS1 INTO XS11
        AT END
            MOVE "Y" TO X1-EMPTY
        NOT AT END
            IF XS11 NOT = wkx THEN
                WRITE X11 FROM XS11
                MOVE XS TO wkx
                ADD 1 TO xcnt
            ELSE
                CONTINUE
            END-IF.

CREATE-Y1LIST SECTION.
    OPEN OUTPUT Y1
    CLOSE Y1

    OPEN EXTEND Y1

    MOVE "NO" TO Y1-EMPTY
    PERFORM UPDATE-Y1LIST
        UNTIL NO-MORE-Y1

    CLOSE Y1.

UPDATE-Y1LIST SECTION.
    RETURN YS1 INTO YS11
        AT END
            MOVE "Y" TO Y1-EMPTY
        NOT AT END
            IF YS11 NOT = wky THEN
                WRITE Y11 FROM YS11
                MOVE YS TO wky
                ADD 1 TO ycnt
            ELSE
                CONTINUE
            END-IF.
