ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1102. Strange Dialog

WA_1:( guys, im trying to solve it for ages, please give me any advice...
Posted by BORODA 10 Dec 2013 21:11
I read 3 chars and run the follow DFA:
                one on in/out put
lost:
empty:
in/out:
input/output:
put:

here is my code:

#include <iostream>
#include <cstdio>
#include <cassert>
#include <cstdlib>
#include <string>

using namespace std;

inline bool good(char C)
{
    return (C >= 'a' && C <= 'z');
}

int a[5][4] = {{0, 0, 0, 0},
               {1, 0, 2, 4},
               {1, 0, 2, 3},
               {1, 1, 2, 4},
               {0, 1, 0, 0}};

char c[3];

bool get_ans()
{
    int n_u = 0, pos = 1;

    if (!good(c[2])) {
        return (c[0] == 'i' && c[1] == 'n');
    }

    bool n2 = 0;
    while (true)
    {
        if (pos == 0) return 0;
        if (n2) return (pos != 0 && pos != 4);

        c[0] = c[n_u % 3];

        for (int i = 3 - n_u; i < 3; i++)
        {
            c[i] = getc(stdin);
            if (!good(c[i])) {
                if (i == 0) return (pos != 0 && pos != 4);
                if (i == 1) return 0;
                n2 = 1;
                break;
            }
        }


        if (!n2 && c[0] == 'o' && c[1] == 'n' && c[2] == 'e') {
            pos = a[pos][0];
            n_u = 3;
            continue;
        }

        if (c[0] == 'o' && c[1] == 'n') {
            pos = a[pos][1];
            n_u = 2;
            continue;
        }

        if (!n2 && c[0] == 'o' && c[1] == 'u' && c[2] == 't') {
            pos = a[pos][2];
            n_u = 3;
            continue;
        }

        if (c[0] == 'i' && c[1] == 'n') {
            pos = a[pos][2];
            n_u = 2;
            continue;
        }

        if (!n2 && c[0] == 'p' && c[1] == 'u' && c[2] == 't') {
            pos = a[pos][3];
            n_u = 3;
            continue;
        }
        return 0;
    }
}

int main()
{
//    assert(freopen("inp.in", "r", stdin) != NULL);

    int n; assert(scanf("%d", &n) == 1);

    c[0] = getc(stdin);

    for (int i = 0; i < n; i++)
    {
        bool ok = 0;
        c[0] = getc(stdin);

        if (good(c[0])) {
            c[1] = getc(stdin);
            if (good(c[1])) {
                c[2] = getc(stdin);
                ok = get_ans();
            } else {
                ok = 0;
            }
        }

        if (ok) printf("YES\n");
        else printf("NO\n");
    }
}

Edited by author 10.12.2013 21:13

Edited by author 10.12.2013 21:15

Edited by author 10.12.2013 21:22