UVa-445 : Marvelous Mazes

問題概要

決められた規則に従って文字列を出力する問題。

解法

基本的にはやるだけ。入力の読み取りが面倒(今思えばJavaでやったほうが良かった気がする)。適宜!をはさむと処理が楽になるかも。

acceptされたコード

計算量O(length)。

#include <cstdio>
#include <string>
#include <iostream>
#include <sstream>
#include <cctype>
using namespace std;

void solve(const string& line){
	const int L = line.length();

	int num = 0;
	for(int i=0; i<L; i++){
		if(isdigit(line[i])){
			num += line[i]&15;
		}
		else if(line[i] == 'b'){
			while(num--){
				putchar(' ');
			}
			num = 0;
		}
		else if(line[i] == '!'){
			putchar('\n');
		}
		else{
			while(num--){
				putchar(line[i]);
			}
			num = 0;
		}
	}
}

int main(){
	string line, str;

	bool first = true;
	for(;;){
		if(!first){
			puts("");
		}
		first = false;

		bool found = true;
		line = "";
		while((found = getline(cin, str))){
			if(!isdigit(str[0])){
				break;
			}
			if(!line.empty() && (line[(int)line.length() - 1] != '!')){
				line += '!';
			}
			line += str;
		}
		if(line[(int)line.length() - 1] != '!'){
			line += '!';
		}
		solve(line);

		if(!found){
			break;
		}
	}

	return 0;
}