[HDU] [1062] Text Reverse

Problem Description

Loading...

Problem Analysis

This was a problem @A_ice gave me that night
Holy crap… I wrote it like a madman past midnight and then turned on mobile data just to upload the code
But the problem itself isn’t especially hard
It’s surprisingly similar to that capitalize-the-first-letter one

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
#include<stdio.h>
#include<string.h>
int main() {
	char s[1200],t;
	int x,i,T,j,k,a,b,p;
	scanf("%d",&T);
	getchar();
	for(i=0; i<T; i++) {
		gets(s);
		k=strlen(s);
		for(a=0,j=0; j<k; j++) {
			if(s[j]==' ') {
				a++;
				b=j;
			} else break;
		}
		for(j=a; j<k; j++) {
			if(s[j]==' ') {
				x=0;
			} else {
				x=1;
				b=j;
			}
			if(x==0||j==k-1) {
				for(p=0; p<(((b-a)/2)+1); p++) {
					t=s[a+p];
					s[a+p]=s[b-p];
					s[b-p]=t;
				}
				a=j+1;
			}
		}
		printf("%s\n",s);
	}
	return 0;
}

Honestly, looking at this code now… it’s only been a little over a month
but I can’t help feeling… could this code still be optimized…

Loading...