Problem Solving Techniques for Error-prone Point Analysis by PAT 1078 String Compression and Decompression C Language

Posted by slevytam on Tue, 01 Oct 2019 21:17:31 +0200

There are many ways to compress text. Here we only consider the simplest one: to represent a continuous fragment of the same character by the number of characters contained in the character and fragment. For example, ccccc is represented by 5c. If the character is not duplicated, it is output as it is. For example, aba is still aba after compression.

The decompression method is reversed by restoring a representation such as 5c to ccccc.

This problem requires you to process a given string according to the requirements of compression or decompression. Here we simply assume that the original string is a non empty string consisting entirely of English letters and spaces.

Input format:

The first line of input gives a character, if C, it means that the following string needs to be compressed; if D, it means that the following string needs to be decompressed. The second line gives a string of no more than 1000 characters to be compressed or decompressed, ending with a carriage return. The Title guarantees that the number of character repetitions is within the integer range and that the output file does not exceed 1MB.

Output format:

Compress or decompress strings as required and output results in one line.

Input Sample 1:

C
TTTTThhiiiis isssss a   tesssst CAaaa as

Output Sample 1:

5T2h4is i5s a3 te4st CA3a as

Input Sample 2:

D
5T2h4is i5s a3 te4st CA3a as10Z

Output Sample 2:

TTTTThhiiiis isssss a   tesssst CAaaa asZZZZZZZZZZ

 

This topic is prone to errors:

1. The number may be ten hundreds, so it is not necessary to read all the numbers for a single character.

2. Compression: There may only be'(blank space) and' n'needs to be cleaned up first.

Skills: Use sscanf and sprintf to skillfully convert numbers and characters.      

 

 

#include<stdio.h>
int main()
{
	char mode = 0;
	scanf("%c",&mode);
	scanf("%*c");
	
	char ch[1024*1025] = {};
	char resu[1024*1025] = {};
	int index = 0;	//Subscript position of resu

	gets(ch);
	if('C' == mode)
	{
		for(int i = 0 ; '\0'!= ch[i] ; i++)
		{
			int count = 1;
			for(i;ch[i] == ch[i+1];i++)	//Read the same characters in succession
			{
				count++;
			}
			if(count > 1)
			{
				char str[256] = {};
				sprintf(str,"%d",count);
				for(int j = 0 ; '\0' != str[j] ; j++)
				{
					resu[index++] = str[j];
				}
				resu[index++] = ch[i];
			}
			else if(1 == count)
			{
				resu[index++] = ch[i];	
			}
		}
		resu[index] = '\0';	//The last character
	}
	else if('D' == mode)
	{
		int i = 0;
		for(i = 0 ; '\0' != ch[i] ; i++)
		{
			int flag = 0;
			char str[256] = {};
			for(i;'0' <= ch[i] && '9' >= ch[i];i++)	//Read all the numbers first
			{
				str[flag++] = ch[i];
			}
			if('0' <= ch[i-1] && '9' >= ch[i-1])	//Turn characters into numbers for looping
			{
				str[flag] = '\0';
				int count = 0;
				sscanf(str,"%d",&count);
				for(int j = 0 ; j < count ; j++)
				{
					resu[index++] = ch[i];
				}
			}
			else	//Not digital direct output
			{
				resu[index++] = ch[i];	
			}
		}
		resu[index++] = '\0';	//Terminator
	}
	printf("%s\n",resu);

	return 0;	
}

 

Topics: Fragment