U E D R , A S I H C RSS

Fmt/문보창

소감

Wrong Answer
뭐가 잘못됐는지 모르겠다.!!!!!!!!!!!! 문제도 애매하고, 원 ㅡㅡ;

코드

~cpp 
// no 848
//#include <fstream>
#include <iostream>
#include <string>
using namespace std;

const int STATE_A = 1;
const int STATE_B = 2;
const int STATE_C = 3;

//fstream fin("input.txt");
//fstream fout("output.txt");

void read_file(string & str);
void remove_enter(string & str);
void restruct_string(string & str);
void remove_string_end_space(string & str);

int main()
{
	string str;
	read_file(str);
	remove_enter(str);
	restruct_string(str);
	remove_string_end_space(str);
	cout << str;
	return 0;
}

void read_file(string & str)
{
	char ch;
	while (1)
	{
		if (cin.peek() == EOF)
			break;
		ch = cin.get();
		str += ch;
	}
}

// step1
void remove_enter(string & str)
{
	int state = STATE_A;
	for (int i = 0; i < str.length(); i++)
	{
		if (state == STATE_A)
		{
			if (str[i] == '\n' || str[i] == ' ')
				continue;
			else
				state = STATE_B;
		}

		else if (state == STATE_B)
		{
			if (str[i] == '\n')
			{
				if (str[i + 1] == ' ' || str[i + 1] == '\n')
					state = STATE_A;
				else
				{
					str[i] = ' ';
					i--;
					while (str[i] == ' ')
					{
						str.erase(&str[i]);
						i--;
					}
					i++;
					state = STATE_A;
				}
			}
		}
	}
}

// step2
void restruct_string(string & str)
{
	int count_char = 0;
	int state = STATE_A;
	bool isEmptyLine = true;
	for (int i = 0; i < str.length(); i++)
	{
		if (state == STATE_A)
		{
			isEmptyLine = true;
			count_char = 0;
			// test
			if (str[i] == '\n')
				continue;
			else if (str[i] != ' ')
				isEmptyLine = false;
			count_char++;
			state = STATE_B;			
		}
		else if (state == STATE_B)
		{
			// 줄 끝의 빈칸을 제거하는것은 빈줄이 아닐때만
			if (str[i] == '\n' && isEmptyLine == false)
			{
				i--;
				while (str[i] == ' ')
				{
					str.erase(&str[i]);
					i--;
				}
				i++;
				state = STATE_A;
				continue;
			}
			// 빈줄일때
			else if (str[i] == '\n' && isEmptyLine == true)
			{
				state = STATE_A;
				continue;
			}
			else if (str[i] != ' ')
				isEmptyLine = false;
			count_char++;

			if (count_char >= 73 && str[i] != ' ')
				state = STATE_C;
		}
		else if (state == STATE_C)
		{

			if (str[i - 1] == ' ')
			{
				i--;
				while (str[i] == ' ' && str[i - 1] == ' ')
				{
					str.erase(&str[i]);
					i--;
				}
				str[i] = '\n';
				state = STATE_A;
			}
			else
			{
				while (1)
				{
					i--;
					if (str[i] == ' ')
					{
						while (str[i] == ' ' && str[i - 1] == ' ')
						{
							str.erase(&str[i]);
							i--;
						}
						str[i] = '\n';
						state = STATE_A;
						break;
					}
					else if (str[i] == '\n' || i == 0)
					{
						while (str[i] != ' ')
							i++;
						while (str[i] == ' ' && str[i] != ' ')
						{
							str.erase(&str[i]);
						}
						str[i] = '\n';
						state = STATE_A;
						break;
					}
				}
			}
		}
	}
}

// step3
void remove_string_end_space(string & str)
{
	int i = str.size() - 1;
	while (str[i] == ' ')
	{
		str.erase(&str[i]);
		i--;
	}
}

나한테 할말

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2009-05-27 07:09:19
Processing time 0.0203 sec