单词博弈

甲乙两个人用一个英语单词玩游戏。两个人轮流进行,每个人每次从中删掉任意一个字母,如果剩余的字母序列是严格单调递增的(按字典序a < b < c <....<z),则这个人胜利。两个人都足够聪明(即如果有赢的方案,都不会选输的方案 ),甲先开始,问他能赢么?

输入: 一连串英文小写字母,长度不超过15,保证最开始的状态不是一个严格单增的序列。

输出:1表示甲可以赢,0表示甲不能赢。

例如: 输入 bad, 则甲可以删掉b或者a,剩余的是ad或者bd,他就赢了,输出1。

又如: 输入 aaa, 则甲只能删掉1个a,乙删掉一个a,剩余1个a,乙获胜,输出0。 

 

解题思路:
1.对于给定的字符串root,甲先处理的结果只有两种可能,必胜或者必败
那么从root中取出一个 字符,一共有四种情况
oot     rot    rot    roo,其中rot情况是重复的,编程的时候可以去掉重复的
2.对上面四个剩余字符串重复步骤1进行判断,判断其中是否有必败字符串,如果有,那么root就肯定为必胜字符串。

public class Chuck
    {
        public static Dictionary<string, int> dictionaryStr =
                new Dictionary<string, int>();

        static void Main(string[] args)
        {
            string word = "abcdcabghwhgaha";
            Console.WriteLine("输入的字符串为{0}", word);
            Stopwatch time = new Stopwatch();
            time.Start();
            who(word);
            //foreach (KeyValuePair<string, int> a in dictionaryStr)
            //{
            //    Console.WriteLine("优先处理字符串{0}的人的结局为{1}", a.Key, a.Value);
            //}
            Console.WriteLine("游戏结果为{0}", dictionaryStr[word]);
            time.Stop();
            Console.WriteLine("运行时间为{0}ms", time.ElapsedMilliseconds);
            Console.ReadLine();
        }

        public static int who(string word)
        {
            bool flag;
            int count;//计算必胜字符串的数量
            if (word.Length == 2)
            {
                if (dictionaryStr.ContainsKey(word) == false)
                {
                    dictionaryStr.Add(word, 1);//只有两个字符构成的字符串为"必胜字符串"
                }
            }
            else
            {
                string str1 = "";
                count = 0;
                for (int i = 0; i < word.Length; i++)
                {
                    str1 = word.Substring(0, i) + word.Substring(i + 1, word.Length - i - 1);
                    flag = Judge(str1);
                    if (flag == true)//从word中删除一个字符,剩下的字符串严格单增
                    {
                        if (dictionaryStr.ContainsKey(word) == false)
                        {
                            dictionaryStr.Add(word, 1);//word为必胜字符串
                            break;
                        }
                    }
                    else
                    {
                        //从word中删除一个字符,剩下的字符串不是严格单增
                        if (dictionaryStr.ContainsKey(str1) == false)//剩下的字符串不在字典里,无法获取字符串是否为必胜字符串
                        {
                            if (who(str1) == 1)//剩下的字符串为必胜字符串
                            {
                                count++;
                            }
                            else
                            {
                                //必败字符串出现,说明word为必胜字符串
                                break;//如果想要得到所有可能的取字符串的方法,可以考虑不break
                            }
                        }
                        else //剩下的字符串存在于字典中,可以直接获取字符串是否为必胜字符串
                        {
                            if (dictionaryStr[str1] == 1)//必胜字符串
                            {
                                count++;
                            }
                            else
                            {
                                //必败字符串出现,说明word为必胜字符串
                                break;//如果想要得到所有可能的取字符串的方法,可以考虑不break
                            }
                        }
                    }

                }
                //从word字符串[word长度为N]取出一个字符后,一共会有N种情况
                //字符串总数N-必胜字符串数=必败字符串数
                if (word.Length - count >= 1)//必败字符串存在,则word为必胜字符串
                {
                    if (dictionaryStr.ContainsKey(word) == false)
                    {
                        dictionaryStr.Add(word, 1);
                    }
                }
                else//必败字符串不存在,则word为必败字符串
                {
                    if (dictionaryStr.ContainsKey(word) == false)
                    {
                        dictionaryStr.Add(word, 0);
                    }
                }
            }
            return dictionaryStr[word];
        }

        /// <summary>
        /// 判断字符串是否是严格递增的
        /// </summary>
        /// <param name="str"></param>
        /// <returns>返回true表示严格单增,返回false表示非严格单增</returns>
        public static bool Judge(string str)
        {
            bool flag = true;//假设字符串是严格单增的
            for (int i = 0; i < str.Length - 1; i++)
            {
                if (str[i] < str[i + 1])
                {

                }
                else
                {
                    flag = false;
                    break;
                }
            }
            return flag;
        }
    }

posted @ 2013-12-26 09:44  ChuckLu  阅读(2356)  评论(0编辑  收藏  举报