Beispiel #1
0
        private _ANSWER solve(string s)
        {
            var val = new string[]
            {
                "ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE"
            };
            var map = new HashMap <char, int>();

            foreach (var x in s)
            {
                map[x]++;
            }
            string ans = null;
            Action <int, string> dfs = null;

            dfs = (k, str) =>
            {
                if (k == 10)
                {
                    if (map.All(x => x.Value == 0))
                    {
                        ans = str;
                    }
                    return;
                }
                {
                    foreach (var c in val[k])
                    {
                        map[c]--;
                    }
                    var ok = true;
                    foreach (var kv in map)
                    {
                        if (kv.Value < 0)
                        {
                            ok = false;
                        }
                    }
                    if (ok)
                    {
                        dfs(k, str + k.ToString());
                    }
                    foreach (var c in val[k])
                    {
                        map[c]++;
                    }
                }
                dfs(k + 1, str);
            };
            dfs(0, "");
            return(ans);
        }
Beispiel #2
0
        public void Solve()
        {
            //??????? x ????????x?a_i ???????????????????
            //b?????????????????
            //+1????????????????…
            var n = ri;
            var a = Enumerate(n, x => rl).ToList();

            a.Add(f(a.ToArray()));
            var b = Enumerate(n, x => rl).ToList();

            b.Add(f(b.ToArray()));

            Debug.WriteLine(a.AsJoinedString());
            Debug.WriteLine(b.AsJoinedString());
            var map = new HashMap <long, int>();

            foreach (var x in a)
            {
                map[x]++;
            }
            foreach (var x in b)
            {
                map[x]--;
            }
            if (map.All(x => x.Value == 0))
            {
                var xs = new List <long>();
                for (int i = 0; i <= n; i++)
                {
                    xs.Add(a[i]);
                }
                xs = xs.Distinct().ToList(); xs.Sort();
                for (int i = 0; i <= n; i++)
                {
                    a[i] = xs.BinarySearch(a[i]);
                    b[i] = xs.BinarySearch(b[i]);
                }
                var m = xs.Count;
                var s = new DisjointSet(m);
                Debug.WriteLine(a.AsJoinedString());
                Debug.WriteLine(b.AsJoinedString());
                for (int i = 0; i <= n; i++)
                {
                    if (a[i] != b[i])
                    {
                        s.Unite((int)a[i], (int)b[i]);
                    }
                }


                var cnt = 0;
                for (int i = 0; i < n; i++)
                {
                    if (a[i] != b[i])
                    {
                        cnt++;
                    }
                }

                if (cnt == 0)
                {
                    IO.Printer.Out.WriteLine(0);
                }
                else
                {
                    for (int i = 0; i < m; i++)
                    {
                        if (s[i] != i)
                        {
                            continue;
                        }
                        if (s.Size(i) == 1)
                        {
                            continue;
                        }
                        if (!s.IsUnited(i, (int)a[n]))
                        {
                            cnt++;
                        }
                    }

                    IO.Printer.Out.WriteLine(cnt);
                }
            }
            else
            {
                IO.Printer.Out.WriteLine(-1);
            }
        }