Sicily 1054. Taxation With Repres

1054. Taxation With Repres

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

In the land of Kalii lived a civilization not unlike our own. They had government, business, and technology. The only major difference was how tax rates were decided. You see, the people of Kalii loved to haggle. They haggled over everything including what percentage of their income went to the government coffers. When a Kalii family's tax contract was up they had to travel to the capital to haggle with government officals and hammer out a new deal. The contract would have a start date, an end date (for bookkeeping purposes the end day was always the 15th of the end month), and a tax rate. It was also traditional for a Kalii family to enjoy a short time period of tax free living between the end of their last contract and the start of the new one. This time period was a point to haggle over as well.

The land of Kalii was prosperous and very populous and, as a consequence, government officials were often overwhelmed with work. When a deal was agreed upon with a family, the officials would quickly write it down in shorthand and send it off to be processed. These scribbled notes had to be converted to XML format to be entered into their computer system. Sometimes the scribble was inconclusive and the family would have to be recalled to the capital for another round of haggling. This entire process was very inefficient but it was the way the people of Kalii had always done it and they were not about to change anytime soon.

Your job is to convert the scribblings of the officials into XML format.

Input

Input consists of multiple datasets, one per line. Each correct line contains three pieces of information:

1. Tax rate

- A tax rate always has a '%' after it if it is a whole number (i.e. 5%).
- A tax rate never has a decimal point in it.
- A tax rate may contain a fraction but its denominator must only be a 2, 4, or 8.
- A tax rate must have a numerator that is less than its denominator.

2. Contract End Date

- A contract end date is a numerical representation of the month (one or two digits) and the last two digits of the year. The month and year may be separated by a single hyphen (i.e. 12-05 or 1205 would refer to 15DEC05).

3. Contract Start Date

- A contract start date is a numerical representation of the month (one or two digits) and a two-digit day of the month. The month and day may be separated by a single hyphen (i.e. 12-05 or 1205 would refer to 05DEC03).
- A contract start date must start on or after the current date. (Suppose it's the beginning of November, 2003.) For example, the current year is 2003. If the start date is 305 it is assumed that the year the contract starts will be 2004.

Note: There may be 0 or more spaces between each of the three fields.
Note: The Kalii calendar consists of the twelve months familiar to you, but all months have 31 days.
Note: The maximum tax rate is 99.875%

Output

For each data set, print the correctly indented XML (4 spaces per indent level):

<Kalii Index=N >
    <startdate>(start date)</startdate>
    <rate>(tax rate)</rate>
    <enddate>(end date)</enddate>
</Kalii>

N should be replaced by the data set number, (start date) by the start date, (tax rate) by the tax rate, and (end date) by the end date. If no output could be computed because of either bad or ambiguous data, simply print <Kalii Index=N >BAD INPUT</Kalii>, where N should be replaced by the data set number.

Note: The format for the start date is DDMMMYY. The format for the end date is 15MMMYY. Where DD is the zero-padded day of month (01-31), MMM is the month in upper-case letters (JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC), and YY is the zero-padded last 2 digits of the year (00-99).
Note: The tax rate should be accurate to 3 decimal places.

Sample Input

25/850812-31
6% 02110122
6.75% 2-11 4-23

Sample Output

<Kalii Index=1>
    <startdate>31DEC03</startdate>
    <rate>2.625</rate>
    <enddate>15MAY08</enddate>
</Kalii>
<Kalii Index=2>
    <startdate>22JAN04</startdate>
    <rate>6.000</rate>
    <enddate>15FEB11</enddate>
</Kalii>

<Kalii Index=3>BAD DATA</Kalii>

// Problem#: 1054
// Submission#: 3457623
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <iomanip>
#include <algorithm>
#include <queue>
#include <functional>
#include <map>
#include <string.h>
#include <math.h>
#include <list>
#include <set>
using namespace std;

int counter = 1;
string p1, p2, p3;
double rate;
double ansR;
int endMonth, endYear, endDay = 15, startMonth, startDay, startYear;
int ansEM, ansEY, ansSM, ansSD, ansSY;
int poss;
char monthName[12][4] = {"JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"};

void judge() {
    //judge part 1
    if (p1[p1.size() - 1] == '%') {
        rate = 0;
        for (int i = 0; i < p1.size() - 1; i++) {
            if (!('0' <= p1[i] && p1[i] <= '9')) return;
            rate = rate * 10 + p1[i] - '0';
        }
    } else {
        if (p1[p1.size() - 1] != '2' && p1[p1.size() - 1] != '4' && p1[p1.size() - 1] != '8') return;
        if ((!('0' <= p1[p1.size() - 3] && p1[p1.size() - 3] <= '9')) || p1[p1.size() - 3] >= p1[p1.size() - 1]) return;
        rate = 0;
        for (int i = 0; i < p1.size() - 3; i++) {
            if (!('0' <= p1[i] && p1[i] <= '9')) return;
            rate = rate * 10 + p1[i] - '0';
        }
        rate += (p1[p1.size() - 3] - '0') * 1.0 / (p1[p1.size() - 1] - '0');
    }
    if (rate > 99.875) return;
    
    //judge part 2 and part 3
    if (p2.size() < 3) return;
    bool part2HasHyphen = false;
    int part2HasHyphenPos;
    for (int i = 0; i < p2.size(); i++) {
        if (p2[i] == '-') {
            part2HasHyphen = true;
            part2HasHyphenPos = i;
        }
    }
    if (part2HasHyphen) {
        endMonth = 0;
        if (part2HasHyphenPos >= p2.size() - 2) return;
        for (int i = 0; i < part2HasHyphenPos; i++) {
            if (p2[i] < '0' || p2[i] > '9') return;
            endMonth = endMonth * 10 + p2[i] - '0';
        }
        endYear = 0;
        for (int i = part2HasHyphenPos + 1; i < p2.size(); i++) {
            if (p2[i] < '0' || p2[i] > '9') return;
            endYear = endYear * 10 + p2[i] - '0';
        }
    } else {
        if (p2.size() > 4) return;
        for (int i = 0; i < p2.size(); i++) 
            if (p2[i] < '0' || p2[i] > '9') return;
        endYear = (p2[p2.size() - 2] - '0') * 10 + p2[p2.size() - 1] - '0';
        if (p2.size() == 3) endMonth = p2[0] - '0';
        else endMonth = (p2[0] - '0') * 10 + p2[1] - '0';
    }
    if (endYear < 3 || endYear > 99) return;
    if (endMonth < 1 || endMonth > 12) return;

    //judge part3
    if (p3.size() < 3) return;
    bool part3HasHyphen = false;
    int part3HasHyphenPos;
    for (int i = 0; i < p3.size(); i++) {
        if (p3[i] == '-') {
            part3HasHyphen = true;
            part3HasHyphenPos = i;
        }
    }
    if (part3HasHyphen) {  // 可以确定part3的值
        startDay = 0;
        if (part3HasHyphenPos >= p3.size() - 2) return;
        for (int i = part3HasHyphenPos + 1; i < p3.size(); i++) {
            if (p3[i] < '0' || p3[i] > '9') return;
            startDay = startDay * 10 + p3[i] - '0';
        }
        startMonth = 0;
        for (int i = 0; i < part3HasHyphenPos; i++) {
            if (p3[i] < '0' || p3[i] > '9') return;
            startMonth = startMonth * 10 + p3[i] - '0';
        }    
    } else {
        if (p3.size() > 4) return;
        for (int i = 0; i < p3.size(); i++) 
            if (p3[i] < '0' || p3[i] > '9') return;
        startDay = (p3[p3.size() - 2] - '0') * 10 + p3[p3.size() - 1] - '0';
        if (p3.size() == 3) startMonth = p3[0] - '0';
        else startMonth = (p3[0] - '0') * 10 + p3[1] - '0';
    }
    if (startDay < 1 || startDay > 31) return;
    if (startMonth < 1 || startMonth > 12) return;
    if (startMonth >= 11) startYear = 3;
    else startYear = 4;

    if (startYear > endYear) return;
    if (startYear == endYear && startMonth > endMonth) return;
    if (startYear == endYear && startMonth == endMonth && startDay >= endDay) return;
    poss++;
    ansEM = endMonth;
    ansEY = endYear;
    ansSD = startDay;
    ansSM = startMonth;
    ansSY = startYear;
    ansR = rate;
}

void output() {
    if (poss == 1) {
        printf("<Kalii Index=%d>\n", counter);
        printf("    <startdate>%02d%s%02d</startdate>\n", ansSD, monthName[ansSM - 1], ansSY);
        printf("    <rate>%.3lf</rate>\n", ansR);
        printf("    <enddate>15%s%02d</enddate>\n", monthName[ansEM - 1], ansEY);
        printf("</Kalii>\n");
    } else {
        printf("<Kalii Index=%d>BAD DATA</Kalii>\n", counter);
    }
    counter++;
}

int main() {

    //std::ios::sync_with_stdio(false);

    char temp[500];

    while (gets(temp)) {
        
        string S(temp);
        int pos1, pos2;
        int s = S.size();
        poss = 0;

        //rate
        bool isOK = false;
        for (int i = 0; i < s; i++) {
            if (S[i] == '%') {
                p1 = S.substr(0, i + 1); 
                pos1 = i + 1;
                isOK = true;
                break;
            }
            if (S[i] == '/') {
                p1 = S.substr(0, i + 2);
                pos1 = i + 2;
                isOK = true;
                break;
            }
        }
        if (!isOK) {
            output();
            continue;
        }

        //endDate
        while (pos1 < s && S[pos1] == ' ') pos1++;
        bool endHasHyphen = false;
        int endHasHyphenPos;
        for (int i = pos1; i < pos1 + 3; i++) {
            if (S[i] == '-') {
                endHasHyphen = true;
                endHasHyphenPos = i;
            }
        }
        int numCounter = 0;
        if (endHasHyphen) {
            p2 = S.substr(pos1, endHasHyphenPos + 2 + 1 - pos1);
            pos2 = endHasHyphenPos + 3;
            while (pos2 < s && S[pos2] == ' ') pos2++;
            p3 = S.substr(pos2, s - pos2);
            judge();
        } else {
            bool existBlank = false;
            for (int i = pos1; i < s; i++) {
                if (S[i] == ' ') {
                    p2 = S.substr(pos1, i - pos1);
                    pos2 = i + 1;
                    while (pos2 < s && S[pos2] == ' ') pos2++;
                    p3 = S.substr(pos2, s - pos2);
                    judge();
                    existBlank = true;
                    break;
                }
            }
            if (!existBlank) {
                //3 number in end
                p2 = S.substr(pos1, 3);
                pos2 = pos1 + 3;
                while (pos2 < s && S[pos2] == ' ') pos2++;
                p3 = S.substr(pos2, s - pos2);
                judge();
                //4 number in end
                p2 = S.substr(pos1, 4);
                pos2 = pos1 + 4;
                while (pos2 < s && S[pos2] == ' ') pos2++;
                p3 = S.substr(pos2, s - pos2);
                judge();
            }
        }
        output();
    }

    return 0;
}                                 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值