[HDU] [2057] A + B Again

Problem Description

Loading...

Problem Analysis

This problem was a bit crazy to work through.

First
This problem can be solved in just a few lines using the built-in hexadecimal conversion.
But since I thought it was a Big Number problem,
I initially chose string simulation.
(After testing, the runtime was about one fifth shorter than the direct version.)

So… let’s put the code first.

cpp
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include<stdio.h>
#include<string.h>
char s[]= {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
char s1[20],s2[20];
int l1,l2;
int find(char c) {
	int i=0;
	while(s[i]!=c)
		i++;
	return i;
}
int vs() {
	if(l1==l2)
		return strcmp(s1,s2);
	else {
		if(l1>l2)
			return 1;
		else
			return -1;
	}
}
int main() {
//	freopen("input.txt","r",stdin);
//	freopen("output.txt","w",stdout);
	int a[20],b[20],sum[20];
	while(scanf("%s %s",s1,s2)!=EOF) {
		char c1='+',c2='+';
		l1=strlen(s1),l2=strlen(s2);
		if(s1[0]=='+') {
			for(int i=0; i<l1-1; i++) {
				s1[i]=s1[i+1];
			}
			l1--;
		}
		if(s1[0]=='-') {
			c1='-';
			for(int i=0; i<l1-1; i++) {
				s1[i]=s1[i+1];
			}
			l1--;
		}
		if(s2[0]=='+') {
			for(int i=0; i<l2-1; i++) {
				s2[i]=s2[i+1];
			}
			l2--;
		}
		if(s2[0]=='-') {
			c2='-';
			for(int i=0; i<l2-1; i++) {
				s2[i]=s2[i+1];
			}
			l2--;
		}
		int t0=0;
		for(int i=0; i<l1; i++) {
			if(s1[i]=='0')
				t0++;
			else
				break;
		}
		if(t0)
			for(int i=0; i<l1-t0; i++) {
				s1[i]=s1[i+t0];
			}
		l1-=t0;
		t0=0;
		for(int i=0; i<l2; i++) {
			if(s2[i]=='0')
				t0++;
			else
				break;
		}
		if(t0)
			for(int i=0; i<l2-t0; i++) {
				s2[i]=s2[i+t0];
			}
		l2-=t0;
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		memset(sum,0,sizeof(sum));
		for(int i=0; i<l1; i++)
			a[i]=find(s1[l1-i-1]);
		for(int i=0; i<l2; i++)
			b[i]=find(s2[l2-i-1]);
		if(c1==c2) {
			int lmax;
			if(l1>l2)
				lmax=l1;
			else
				lmax=l2;
			for(int i=0; i<lmax; i++) {
				if(sum[i]+a[i]+b[i]<16)
					sum[i]+=a[i]+b[i];
				else {
					sum[i+1]=1;
					sum[i]+=a[i]+b[i]-16;
				}
			}
			if(sum[lmax]==1)
				lmax++;
			if(lmax==0)
				printf("0\n");
			else {
				if(c1=='-')
					printf("%c",c1);
				bool flag=false;
				for(int i=lmax-1; i>=0; i--) {
					if(s[sum[i]]!='0') {
						printf("%c",s[sum[i]]);
						flag=true;
					} else {
						if(flag)
							printf("%c",s[sum[i]]);
					}
				}
				printf("\n");
			}
		} else {
			int tvs=vs();
			if(tvs==0)
				printf("0\n");
			else {
				int lmax;
				if(tvs>0) {
					if(l1>l2)
						lmax=l1;
					else
						lmax=l2;
					for(int i=0; i<lmax; i++) {
						if(a[i]-b[i]<0) {
							a[i+1]--;
							sum[i]=a[i]+16-b[i];
						} else
							sum[i]=a[i]-b[i];
					}
				} else {
					if(l1>l2)
						lmax=l1;
					else
						lmax=l2;
					for(int i=0; i<lmax; i++) {
						if(b[i]-a[i]<0) {
							b[i+1]--;
							sum[i]=b[i]+16-a[i];
						} else
							sum[i]=b[i]-a[i];
					}
				}
				while(sum[lmax-1]==0&&lmax>0)
					lmax--;
				if(lmax==0)
					printf("0\n");
				else {
					if(tvs>0&&c1=='-')
						printf("%c",c1);
					else if(tvs<0&&c2=='-')
						printf("%c",c2);
					bool flag=false;
					for(int i=lmax-1; i>=0; i--) {
						if(s[sum[i]]!='0') {
							printf("%c",s[sum[i]]);
							flag=true;
						} else {
							if(flag)
								printf("%c",s[sum[i]]);
						}
					}
					printf("\n");
				}
			}
		}
		memset(s1,0,sizeof(s1));
		memset(s2,0,sizeof(s2));
	}
	return 0;
}

Once the general idea is clear,
writing it step by step is actually not too bad,
but I still got a lot of WAs
from all kinds of detail issues.

I think the highlight of this code is storing hexadecimal digits in an array,
and then using that array directly whenever conversion is needed.

In addition, I think the biggest takeaway was learning to generate my own test data.
Here is a piece of code I wrote to generate input data.

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<stdio.h>
#include <windows.h>
#include <time.h>
char s[]= {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
int main() {
//freopen("input.txt","r",stdin);
	freopen("input.txt","w",stdout);
	srand((unsigned)time(NULL));
	for(int T=0; T<100000; T++) {
		if(rand()%2)
			if(rand()%2)
				printf("+");
			else
				printf("-");
		int l;
		l=rand()%16+1;
		for(int i=0; i<l; i++)
			printf("%c",s[rand()%16]);
		printf(" ");
		if(rand()%2)
			if(rand()%2)
				printf("+");
			else
				printf("-");
		l=rand()%16+1;
		for(int i=0; i<l; i++)
			printf("%c",s[rand()%16]);
		printf("\n");
	}
	return 0;
}

After running, the input data will be saved as
input.txt

Then add the following to your own code

freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);

Then you can read data from input.txt
output it to output.txt
and with the data output by the reference solution and a text comparison tool (I used TextDiff)
you can quickly find errors.

PS: Remember to delete those two test lines when submitting
otherwise you will get WA
(I forgot to delete them at the beginning.)

Anyway, leaving other things aside,
I still gained a lot from this problem.

Problem link: HDU[2057]A + B Again

Loading...