现在的位置: 首页 > 综合 > 正文

HDOJ 4891 The Great Pan(读入优化)

2018年02月21日 ⁄ 综合 ⁄ 共 1053字 ⁄ 字号 评论关闭
 一开始tle和wa了很多发,最后才发现是自己默认数字只有一位,提醒一下大家吧。
 如果以后自己用getchar读数字的时候,记得数字是有好几位的

值得赞的是读入函数,跟龙学的,一个字,快。

AC代码:
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <assert.h>
#include <algorithm>
#define MAX 1234567890
#define MIN -1234567890
#define eps 1e-8
#define CONST 1e5

using namespace std;

char text[1010000];
int len;
int i;
__int64 ans;

char readin()
{
       char ch;
       int n = 0;
       if((ch = getchar()) == EOF) return ch;
       while((ch >= '0') && (ch <= '9'))
       {
                n *= 10;
                n += ch - '0';
                ch = getchar();
       }
       len = 0;
       int i = 1;
       while(i <= n)
       {
               if ((ch = getchar()) == '\n') i++;
               else text[len++] = ch;
       }
       text[len] = '\0';
       return ch;
}

void Brace()
{
        i++;
        int cnt = 0;
        while(text[i] != '}')
        {
                if(text[i] == '|') cnt++;
                i++;
        }
        ans *= cnt+1;
        if(ans > CONST) ans = 0;
}

void Dollar()
{
        i++;
        while(text[i] != '$')
        {
                int cnt = 0;
                while(text[i] == ' ')
                {
                        cnt++;
                        i++;
                }
                if(cnt)
                {
                        ans *= cnt+1;
                        if(ans > CONST) ans = 0;
                        i--;
                }
                i++;
        }
}


int main()
{
        #ifdef BellWind
        freopen("4891.in", "r", stdin);
        #endif // BellWind

        while(~readin())
        {
                for(i = 0, ans = 1; i < len; i++)
                {
                        if(text[i] == '{') {Brace();}
                        if(text[i] == '$') {Dollar();}
                }
                if(ans) printf("%I64d\n", ans);
                else printf("doge\n");
//                printf("%s\n", text);
        }

        return 0;
}

抱歉!评论已关闭.