您的位置:首页 > 其它

HDU 1073 Online Judge(字符串输入方面的经验)

2014-08-23 10:10 465 查看

题目大意:

判断两个表达式的相同情况,如果完全相同,则输出”Accepted“,如果只有空格(‘ ’)、换行符(‘\n’)和制表符('\t')不同,则输出”Presentation Error“,否则输出”Wrong
Answer“。每个表达式都位于一对”START“和”END“之间。

题目分析:

一到字符串处理的水题,不过我以前是习惯单个字符的输入(c=getchar()),对于这道题无能为力,过了很多的测试样例,不知道错误出在哪里。后来改为用gets()函数接收一行的字符。很容易过了这道题。

代码:

单个字符的接收(Wrong Answer)

#include <cstdio>
#include <cstring>

char str1[10000], str2[10000];
int len1, len2;

char start1[20], end1[20];
char start2[20], end2[20];

int nextChar(char str[], int i) {
int len = strlen(str);
while(i<len) {
if(str[i] == ' ' || str[i] == '\t' || str[i] == '\n') {
i ++;
} else break;
}
if(i == len) return -1;
return i;
}

int main() {
int t, c;
scanf("%d", &t);
while(t--) {
memset(str1, 0, sizeof(str1));
memset(str2, 0, sizeof(str2));
scanf("%s", start1);
getchar();
len1 = 0;
len2 = 0;
while(true) {
c = getchar();
if(c == 'E') break;
str1[len1++] = c;
}
str1[len1] = '\0';
scanf("%s", end1);

scanf("%s", start2);
getchar();
while(true) {
c = getchar();
if(c == 'E') break;
str2[len2++] = c;
}
str2[len2] = '\0';
scanf("%s", end2);

if(strcmp(str1, str2) == 0) {
printf("Accepted\n");
continue;
}
int flag = 0;
int p, q;
p = nextChar(str1, 0);
q = nextChar(str2, 0);
while(p != -1 && q != -1) {
if(str1[p] != str2[q]) {
flag = 1;
break;
}
p = nextChar(str1, p+1);
q = nextChar(str2, q+1);
}
if(flag == 1 || q + p != -2) {
printf("Wrong Answer\n");
} else printf("Presentation Error\n");
}

return 0;

接收一行的字符(Accepted)

#include <cstdio>
#include <cstring>
#define M 10000

char str1[M], str2[M];
char tmp_str1[M], tmp_str2[M];
char line[M];

int main() {
int t, i, j;
scanf("%d", &t);
while(t--) {
memset(str1, '\0', sizeof(str1));
memset(str2, '\0', sizeof(str2));
memset(tmp_str1, '\0', sizeof(tmp_str1));
memset(tmp_str2, '\0', sizeof(tmp_str2));
while(gets(line) && strcmp(line, "START") != 0) {
}
while(gets(line) && strcmp(line, "END") != 0) {
if(strlen(line) == 0) {
strcat(str1, "\n");
}
else strcat(str1, line);
}
while(gets(line) && strcmp(line, "START") != 0) {

}
while(gets(line) && strcmp(line, "END") != 0) {
if(strlen(line) == 0) {
strcat(str2, "\n");
}
else strcat(str2, line);
}
if(strcmp(str1, str2) == 0) {
printf("Accepted\n");
continue;
}
int len1=0, len2=0;
i = 0; j = 0;
while(str1[i] != '\0') {
if(str1[i] != ' ' && str1[i] != '\n' && str1[i] != '\t') {
tmp_str1[len1++] = str1[i];
}
i++;
}
i = 0;
while(str2[i] != '\0') {
if(str2[i] != ' ' && str2[i] != '\n' && str2[i] != '\t') {
tmp_str2[len2++] = str2[i];
}
i++;
}
if(strcmp(tmp_str1, tmp_str2) == 0) {
printf("Presentation Error\n");
}
else printf("Wrong Answer\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: