HDU 1073 Online Judge (字符串处理)

本文介绍了一个简单的在线判题系统实现方法,主要讲解如何通过比较标准输出与用户提交的答案来判断提交结果,包括Accepted、Presentation Error及Wrong Answer的判定。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接

Problem Description

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

分析:
首先是对于数据的输入部分,只能够用gets()来一行一行的读入,不能用scanf(),也不能一个字符一个字符的读入(scanf()是因为遇到空格就自动结束了,最后字符串中间的空格都没有读进去;如果一个字符一个字符的读的话,可能中间会出现"END"子串,让输入在不该结束的地方提前结束了),这样才能保证读到完全正确的data数据部分。

最主要的是运用一些字符串处理的函数,就很容易了。

代码:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<string>
using namespace std;
char a[5009],b[5009];
void trim(char *s)//对字符串进行处理,去掉里面的空格、换行和tabs
{
    char temp[5009];
    int j=0;
    for(int i=0; i<strlen(s); i++)
    {
        if(s[i]!=' '&&s[i]!='\t'&&s[i]!='\n')
        {
            temp[j]=s[i];
            j++;
        }
    }
    temp[j]='\0';
    strcpy(s,temp);
}
void Compare(char *a,char *b)
{
    if (strcmp(a,b)==0)
    {
        printf("Accepted\n");
        return ;
    }
    trim(a);
    trim(b);
    if (strcmp(a,b)==0)//去掉空格、换行、tabs后相等
    {
        printf("Presentation Error\n");
        return ;
    }
    else
    {
        printf("Wrong Answer\n");
        return ;
    }
}
void input(char * s)
{
    char temp[5009];
    gets(temp);
    if(strcmp(temp,"START")==0)//从此处才开始得到data部分
    {
        gets(temp);//注意这里一定要用gets()来进行读入,因为scanf()不能读入空格
        while(strcmp(temp,"END")!=0)
        {
            if(strlen(temp)==0)
                strcat(s,"\n");//字符串追加
            else
                strcat(s,temp);
            gets(temp);
        }
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    getchar();
    while(T--)
    {
        strcpy(a,"\0");//对a字符串进行刷新
        strcpy(b,"\0");//对b字符串进行刷新
        input(a);
        input(b);
        Compare(a,b);
    }
    return 0;
}

转载于:https://www.cnblogs.com/cmmdc/p/7671349.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值