POJ 1782 解题

被一个破空格弄惨了,悲剧,以后还是申请一个大字符数组比较好做

#include<iostream>
#include<stdio.h>

using namespace std;

#define NOT_CONS 0 //0: not consecutive;
#define CONS 1 //1: consecutive;
#define NOT_CONS_FIRST 2 //2: not consecutive and 1st char

void init(char& in,char& last,int& lastLen,int& state){
	while(true){
		in = getchar();
		if(in==10){
			cout<<endl;
			continue;
		}
		else break;
	}
	last = in;
	lastLen=1;
	state=NOT_CONS_FIRST;
}

int main(){

	int state;  

	char in,last;
	int lastLen;

	//init
	init(in,last,lastLen,state);

	while(in>=0){
		in = getchar();
		if(in==10){//end situation
			if(state==NOT_CONS_FIRST){
				cout<<'1';
				state=NOT_CONS;
			}
			if(state==NOT_CONS){
				if(last=='1')
					cout<<'1';
				cout<<last<<lastLen<<endl;
			}else{
				cout<<lastLen<<last<<endl;
			}
			//reset
			init(in,last,lastLen,state);

		}else if(in==last){//maybe consecutive seq
			if(state==NOT_CONS){
				cout<<'1';//end 1
			}
			state = CONS;
			if(lastLen<9){
				lastLen++;
			}else{
				cout<<'9'<<last;
				lastLen=1;
				state=NOT_CONS_FIRST;
			}
		}else{//last!=in not consecutive seq
			if(state==NOT_CONS_FIRST){
				cout<<'1';
				state=NOT_CONS;
			}
			if(state==NOT_CONS){
				if(last=='1') cout<<'1';
				cout<<last;
				last=in;
				lastLen=1;
			}else if(state==CONS){
				cout<<lastLen<<last;
				last=in;
				lastLen=1;
				state=NOT_CONS_FIRST;
			}
		}//end else
	}//end while
	
	return 0;
}
posted on 2011-05-08 18:03  超级福满多  阅读(259)  评论(0编辑  收藏  举报