Submission #1120623


Source Code Expand

using System;
using System.Collections.Generic;
using System.Linq;

class Program {
    static int ReadInt() { return int.Parse(Console.ReadLine()); }
    static int[] ReadInts() { return Console.ReadLine().Split().Select(int.Parse).ToArray(); }
    static string[] ReadStrings() { return Console.ReadLine().Split(); }

    const int Sheep = 0;
    const int Wolf = 1;

    static bool Validate(int p, int[] xs, char reply) {
        int prev = (p - 1 + xs.Length) % xs.Length;
        int next = (p + 1) % xs.Length;

        if (xs[p] == Sheep) {
            return reply == 'o' ? xs[prev] == xs[next] : xs[prev] != xs[next];
        }
        else {
            return reply == 'o' ? xs[prev] != xs[next] : xs[prev] == xs[next];
        }
    }

    static bool Calc_1(int[] xs, string s) {

        Func<int, char, int, int> other = (animal, reply, prev) => {
            if (animal == Sheep) {
                return reply == 'o' ? prev : (prev ^ 1);
            }
            else { // Wolf
                return reply == 'o' ? (prev ^ 1) : prev;
            }
        };

        int n = s.Length;
        for (int i = 1; i < n - 1; i++) {
            int prev = xs[i - 1];
            xs[i + 1] = other(xs[i], s[i], prev);
        }

        var a = Validate(0, xs, s[0]);
        var b = Validate(n - 1, xs, s[n - 1]);
        var c = Validate(n - 2, xs, s[n - 2]);
        return a && b && c;
    }

    static void Calc(string s) {
        int n = s.Length;

        bool found = false;
        var xs = new int[n];
        for (int i = 0; i < (1 << 2); i++) {
            // (S, S), (S, W), (W, S), (W, W) の 4 パターン試す
            xs[0] = (i & 2) == 0 ? 0 : 1;
            xs[1] = i & 1;
            if (Calc_1(xs, s)) {
                Console.WriteLine(string.Join("", xs.Select(e => e == Sheep ? "S" : "W")));
                found = true;
                break;
            }
        }

        if (!found)
            Console.WriteLine(-1);
    }

    static void Main() {
        ReadInt();
        var s = Console.ReadLine();
        Calc(s);
    }
}

Submission Info

Submission Time
Task D - Menagerie
User noriok
Language C# (Mono 4.6.2.0)
Score 500
Code Size 2154 Byte
Status AC
Exec Time 31 ms
Memory 12512 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 3
AC × 16
Set Name Test Cases
Sample 00_example_01.txt, 00_example_02.txt, 00_example_03.txt
All 00_example_01.txt, 00_example_02.txt, 00_example_03.txt, 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt
Case Name Status Exec Time Memory
00_example_01.txt AC 22 ms 9172 KB
00_example_02.txt AC 21 ms 9172 KB
00_example_03.txt AC 24 ms 11220 KB
01.txt AC 27 ms 9952 KB
02.txt AC 25 ms 9696 KB
03.txt AC 21 ms 9152 KB
04.txt AC 23 ms 11220 KB
05.txt AC 29 ms 12384 KB
06.txt AC 27 ms 10336 KB
07.txt AC 24 ms 11256 KB
08.txt AC 23 ms 9300 KB
09.txt AC 23 ms 9236 KB
10.txt AC 22 ms 9132 KB
11.txt AC 31 ms 12512 KB
12.txt AC 31 ms 10464 KB
13.txt AC 30 ms 10464 KB