POJ 1008 解题分析

Technorati 标签: ACM,POJ

题目描述

原题链接:POJ 1008 Maya Calendar

Time Limit:
1000ms
Memory limit:
10000kB
题目描述
During his last sabbatical, professor M. A. Ya made a surprising discovery about the old Maya calendar. From an old knotted message, professor discovered that the Maya civilization used a 365 day long year, called Haab, which had 19 months. Each of the first 18 months was 20 days long, and the names of the months were pop, no, zip, zotz, tzec, xul, yoxkin, mol, chen, yax, zac, ceh, mac, kankin, muan, pax, koyab, cumhu. Instead of having names, the days of the months were denoted by numbers starting from 0 to 19. The last month of Haab was called uayet and had 5 days denoted by numbers 0, 1, 2, 3, 4. The Maya believed that this month was unlucky, the court of justice was not in session, the trade stopped, people did not even sweep the floor.
For religious purposes, the Maya used another calendar in which the year was called Tzolkin (holly year). The year was divided into thirteen periods, each 20 days long. Each day was denoted by a pair consisting of a number and the name of the day. They used 20 names: imix, ik, akbal, kan, chicchan, cimi, manik, lamat, muluk, ok, chuen, eb, ben, ix, mem, cib, caban, eznab, canac, ahau and 13 numbers; both in cycles.
Notice that each day has an unambiguous description. For example, at the beginning of the year the days were described as follows:
1 imix, 2 ik, 3 akbal, 4 kan, 5 chicchan, 6 cimi, 7 manik, 8 lamat, 9 muluk, 10 ok, 11 chuen, 12 eb, 13 ben, 1 ix, 2 mem, 3 cib, 4 caban, 5 eznab, 6 canac, 7 ahau, and again in the next period 8 imix, 9 ik, 10 akbal . . .
Years (both Haab and Tzolkin) were denoted by numbers 0, 1, : : : , where the number 0 was the beginning of the world. Thus, the first day was:
Haab: 0. pop 0
Tzolkin: 1 imix 0
Help professor M. A. Ya and write a program for him to convert the dates from the Haab calendar to the Tzolkin calendar.
输入
The date in Haab is given in the following format:
NumberOfTheDay. Month Year
The first line of the input file contains the number of the input dates in the file. The next n lines contain n dates in the Haab calendar format, each in separate line. The year is smaller then 5000.
输出
The date in Tzolkin should be in the following format:
Number NameOfTheDay Year
The first line of the output file contains the number of the output dates. In the next n lines, there are dates in the Tzolkin calendar format, in the order corresponding to the input dates.
样例输入
3
10. zac 0
0. pop 0
10. zac 1995
样例输出
3
3 chuen 0
1 imix 0
9 cimi 2801
Global No.
10

解题分析

这道题比较简单,需要注意的是输入的时候,日期后面有个小点,不注意容易出错。

C++的switch-case不能应用在string上面,这点让人十分不爽,对输入中的月份名称的辨认,我是使用的有穷自动机识别的。现拟制作一个代码生成器,根据字符串序列生成这些字符串的识别自动机,返回这些字符串的enum,然后即可利用C++的switch-case进行处理。

等有空的时候用lex和yacc好好弄一下的,现在先用C#写个不完全版的凑合一下:

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

namespace CPPStringSwitchCase
{
    class Program
    {
        static void Trans(IEnumerable<string> KeyWords, int length)
        {
            var GroupedKeyWords = KeyWords.Where(key=>key.Length > length).GroupBy(key => key[length]);
            if (GroupedKeyWords.Count() == 0) return;

            string prefix = "";
            for (int i = 0; i < length + 1; i++)
                prefix += "\t";

            Console.WriteLine(String.Concat(prefix, "switch(input[", length, "]) {"));
            foreach (IGrouping<char, string> GroupedKeyWord in GroupedKeyWords)
            {
                Console.WriteLine(String.Concat(prefix, "case ", GroupedKeyWord.Key, ":"));
                Trans(GroupedKeyWord, length + 1);
                Console.WriteLine(String.Concat(prefix, "\tbreak;"));
            }
            Console.WriteLine(String.Concat(prefix, "default: return NOT_FOUND;"));
            Console.WriteLine(String.Concat(prefix, "}"));

        }

        static void Main(string[] args)
        {
            string EnumName = "XX";

            List<string> KeyWords = new List<string>();
            while (true)
            {
                string tmp = Console.ReadLine();
                if (string.IsNullOrEmpty(tmp))
                    break;

                KeyWords.Add(tmp);
            }

            Console.WriteLine("enum {0}", EnumName);
            Console.WriteLine("{");
            foreach (string keyWord in KeyWords)
                Console.WriteLine("\t{0},", keyWord);
            Console.WriteLine("\t{0}", "NOT_FOUND");
            Console.WriteLine("}");

            Console.WriteLine(String.Concat(EnumName, " Map", EnumName, "(const char & input) {"));
            Trans(KeyWords, 0);
            Console.WriteLine("}");
        }
    }
}

总结

总结等以后用重做小工具后再补吧,最近好忙>_<

posted @ 2010-07-15 14:47  HCOONa  阅读(364)  评论(0编辑  收藏  举报