Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
贴个用有限状态机的S0为空 S1为接收不同字符 S2为接收相同字符 代码如下: #include <cstdio> #include <string> using namespace std; void outputSingles(const string& s) { if(s.empty()) return; putchar('1'); for(string::size_type i = 0, len = s.size(); i < len; ++i){ putchar(s[i]); if(s[i] == '1') putchar('1'); } putchar('1'); } void outputSames(const string& s) { if(s.empty()) return; printf("%u%c", s.size(), s[0]); } int main() { string s; int state = 0, c; s.reserve(100); while((c = getchar()) != -1){ switch(state){ case 0: s.clear(); if(c == '\n') putchar('\n'); else{ s.push_back(c); state = 1; } break; case 1: if(c == '\n'){ outputSingles(s); putchar('\n'); state = 0; } else if(c != s[s.size()-1]) s.push_back(c); else{ outputSingles(s.substr(0, s.size()-1)); s = s.substr(s.size()-1); s.push_back(c); state = 2; } break; case 2: if(c == '\n'){ outputSames(s); putchar('\n'); state = 0; } else if(c == s[s.size()-1]) s.push_back(c); else{ outputSames(s); s.clear(); s.push_back(c); state = 1; } if(s.size() == 9){ //if length is 9, output this part outputSames(s); state = 0; } break; } } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator