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 |
贴下代码 一次ac#include <iostream> #include <string> #include <vector> using namespace std; unsigned getLength(vector<string>& vps) { unsigned length=1; for(unsigned pos=2; pos<vps.size(); ++pos) { for(length=1; length<=pos; ++length) if(vps[pos-length]==vps[pos]) return length; } return 0; } int main() { string n, vnp, vnq; vnp.reserve(100), vnq.reserve(100); //最长给出的字符串不会超过80个 vector<int> note; //用于记录下一次字符串的形态 vector<string> vps; //记录每次转换的字符串形态 while(cin>>n, n!="-1") { vps.clear(); vnp=n, vnq=vnp; int iterations=0, steps=0, length=0; while(iterations!=15) { vnq=vnp; note.assign(20, 0); //记录下次转换的字符串形态 for(string::size_type i=0; i<vnp.size(); ++i) { int idx=vnp[i]-'0'; note[idx]++; } vnp.clear(); for(int idx=0; idx<10; ++idx) { if(note[idx]==0) continue; if(note[idx]>=10 ) { char c1=note[idx]/10+'0', c2=note[idx]%10+'0'; vnp.push_back(c1); vnp.push_back(c2); } else vnp.push_back(note[idx]+'0'); vnp.push_back(idx+'0'); } ++steps; if(vnp==vnq) break; vps.push_back(vnp); if(length=(int)getLength(vps)) break; ++iterations; } if(steps==1) cout<<n<<" is self-inventorying"<<endl; else if(iterations==15) cout<<n<<" can not be classified after 15 iterations"<<endl; else if(vnp==vnq) cout<<n<<" is self-inventorying after "<<steps-1<<" steps"<<endl; else cout<<n<<" enters an inventory loop of length "<<length<<endl; } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator