HDU-1703 Online Judge (练习处理输入)

一、题目概述

 
Ignatius is building an Online Judge, now he has worked out all the problems except the Judge System. The system has to read data from correct output file and user's result file, then the system compare the two files. If the two files are absolutly same, then the Judge System return "Accepted", else if the only differences between the two files are spaces(' '), tabs('\t'), or enters('\n'), the Judge System should return "Presentation Error", else the system will return "Wrong Answer".

Given the data of correct output file and the data of user's result file, your task is to determine which result the Judge System will return.
 

 

Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case has two parts, the data of correct output file and the data of the user's result file. Both of them are starts with a single line contains a string "START" and end with a single line contains a string "END", these two strings are not the data. In other words, the data is between the two strings. The data will at most 5000 characters.
 

 

Output
For each test cases, you should output the the result Judge System should return.
 

 

Sample Input
4
START
1 + 2 = 3
END
START
1+2=3
END
 
 
START
1 + 2 = 3
END
START
1 + 2 = 3
END
 
 
START
1 + 2 = 3
END
START
1 + 2 = 4
END
 
 
START
1 + 2 = 3
END
START
1 + 2 = 3
END
 

 

Sample Output
Presentation Error
Presentation Error
Wrong Answer
Presentation Error

 

二、题目释义

获取输入,比对两个字符串,根据结果输出不同的答案

三、思路分析

这道题关键在于怎么处理输入,这里就需要许多对输入、字符、字符串的处理,一个一个读字符可以读取所有(回车符什么的都可以),但这样做这道题效率会偏低,这里我们用cin.getline()来读取整行,用strcmp()来做读取时转存的比较,用strcat()保存完成的字符串拼接,以正确获取两个answer。之后再做判断处理,判断相对读取是比较简单的。

四、AC代码

代码借鉴自其他博主

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <cstring>
using namespace std;

const int N = 5050;
void input(char a[]);
void simplify(char a[]);
int cmp(char a[],char b[]);
char s1[N]={0},s2[N]={0};

int main(){
    int n;
    scanf("%d",&n);
    while(n--)
    {
        memset(s1,0,sizeof(s1));
        memset(s2,0,sizeof(s2));
        input(s1),input(s2);
        int m = cmp(s1,s2);
        switch(m)
        {
        case 1:
            printf("Accepted\n");break;
        case 2:
            printf("Presentation Error\n");break;
        case 3:
            printf("Wrong Answer\n");break;
        }
    }
    return 0;
}
void input(char a[])
{
    char s[N];
    while(scanf("%s",s),strcmp(s,"START"));
    while(cin.getline(s,N),strcmp(s,"END"))
    {
        if(s[0] != '\0')
            strcat(a,s);
        else strcat(a,"\n");
    }
}

void simplify(char a[])
{
    int i,k=0;
    char s[N];
    for(i=0; a[i]!='\0'; i++)
    {
        if(a[i]==' '||a[i]=='\n'||a[i]=='\t')
            continue;
        s[k++]=a[i];
    }
    s[k++]='\0';
    strcpy(a,s);
}

int cmp(char a[],char b[])
{
    if(!strcmp(a,b))
        return 1;
    simplify(a),simplify(b);
    if(!strcmp(a,b))
        return 2;
    return 3;
}

 

posted @ 2018-01-24 19:13  Nevercome  阅读(488)  评论(0编辑  收藏  举报