一道模拟题,用的vector比用链表要方便很多,毕竟不需要自己写,因为是递增的,所以每一次你都要去检查最后一位加1之后有没有越界,如果没越界你就可以把他当前的字符删掉替换成他下一位的字符就可以了,如果越界了那么直接判断输出结束就可以了。
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<vector> using namespace std; int s, t, w; vector<char>a; char total_last;//最后一位字符 int p;//判断变量 bool next(int end,int len){ char last='a'+end-1;//序列中的最后一个 while(a.size()<=len) { if(a.back()==last) { a.pop_back();//去掉最后一个 --last;//最后一位向前移动 } else { char temp=++a.back(); if(temp>total_last)//越界判断 p=1; while(a.size()<len){ a.push_back(++temp); } return true; } } return false; } int main() { cin>>s>>t>>w; for(int i=0;i<w;i++){ char c; cin>>c; a.push_back(c); } total_last='a'+t-1; p=0; for(int i=1;next(t,w)&&i<=5;i++) { if(p) continue; for(vector<char>::iterator ite=a.begin();ite!=a.end();ite++){ cout<<*ite; } cout<<endl; } return 0; }