1. 程式人生 > >HDU-1703 Online Judge (練習處理輸入)

HDU-1703 Online Judge (練習處理輸入)

itl panel sent 獲取 練習 acc iostream rec sin

一、題目概述

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;
}

HDU-1703 Online Judge (練習處理輸入)