http://acm.hdu.edu.cn/showproblem.php?pid=1073
以后的串操作还是靠char最靠谱。。这是个超水的题。
#include <iostream>
using namespace std;
#define MAX 5003
char temp[MAX],a[MAX],b[MAX];
void trim(char *s){ //这才是trim的王道函数,一个char走遍全天下。。
char temp[MAX];
int i,j=0;
strcpy(temp,"\0");
for (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){
printf("Presentation Error\n");
return ;
}
else {
printf("Wrong Answer\n");
return ;
}
}
void input(char *s){
char temp[MAX];
strcmp(temp,"\0");
gets(temp);
if (strcmp(temp,"START")==0){
gets(temp);
while(strcmp(temp,"END")!=0){ //回车可以pass过gets和getchar,而%s和cin会被挡住
if (strlen(temp)==0) //pass过后,gets得到的字符串为空,并没有实际接受回车。
strcat(s,"\n");
else strcat(s,temp);
gets(temp);
}
}
}
int main(){
int n;
scanf("%d",&n);
getchar();
while (n--){
strcpy(a,"\0");
strcpy(b,"\0");
input(a);
input(b);
Compare(a,b);
}
return 0;
}
顺便复习一下有关char的几个函数,从网上找到个一个比较全面的函数操作。
程序开头要声明
#include <string.h>
函数名: stpcpy
功 能: 拷贝一个字符串到另一个
用 法: char *stpcpy(char *destin, char *source);
程序例:
#include <stdio.h>
#include <string.h>
int main(void)
{
char string[10];
char *str1 = "abcdefghi";
stpcpy(string, str1);
printf("%s\n", string);
return 0;
}
函数名: strcat
功 能: 字符串拼接函数
用 法: char *strcat(char *destin, char *source);
程序例:
#include <string.h>
#include <stdio.h>
int main(void)
{
char destination[25];
char *blank = " ", *c = "C++", *Borland = "Borland";
strcpy(destination, Borland);
strcat(destination, blank);
strcat(destination, c);
printf("%s\n", destination);
return 0;
}
函数名: strcmp
功 能: 串比较
用 法: int strcmp(char *str1, char *str2);
看Asic码,str1>str2,返回值 > 0;两串相等,返回0
程序例:
#include <string.h>
#include <stdio.h>
int main(void)
{
char *buf1 = "aaa", *buf2 = "bbb", *buf3 = "ccc";
int ptr;
ptr = strcmp(buf2, buf1);
if (ptr > 0)
printf("buffer 2 is greater than buffer 1\n");
else
printf("buffer 2 is less than buffer 1\n");
ptr = strcmp(buf2, buf3);
if (ptr > 0)
printf("buffer 2 is greater than buffer 3\n");
else
printf("buffer 2 is less than buffer 3\n");
return 0;
}
函数名: strcpy
功 能: 串拷贝
用 法: char *strcpy(char *str1, char *str2);
程序例:
#include <stdio.h>
#include <string.h>
int main(void)
{
char string[10];
char *str1 = "abcdefghi";
strcpy(string, str1);
printf("%s\n", string);
return 0;
}