#include <iostream>
#include <stack>
#include <list>
#include <vector>
using namespace std;
string s;
struct card
{
char color;
char rank_;
};
struct pile
{
stack<card> s;
};
list<pile> pilelist;
card c;
pile p;
list<pile>::iterator i;
list<pile>::iterator it;
void move_card()
{
//cout<<it->s.top().rank_<<it->s.top().color<<" "<<endl;
// cout<<i->s.top().rank_<<i->s.top().color<<" "<<endl;
card tmp=(*it).s.top();
(*it).s.pop();
if(it->s.empty()) pilelist.erase(it);
(*i).s.push(tmp);
/* list<pile>::iterator it1=pilelist.begin();
for(;it1!=pilelist.end();it1++)
cout<<it1->s.top().rank_<<it1->s.top().color<<" ";
cout<<endl;*/
}
bool find_equal_card()
{
if(i--==pilelist.begin()) return false;
list<pile>::iterator i1=i;
if(i==pilelist.begin())
{
if((*i).s.top().rank_==(*it).s.top().rank_ || (*i).s.top().color==(*it).s.top().color )
return true;
else
return false;
}
i--;
if(i!=pilelist.begin())
{
i--;
if((*i).s.top().rank_==(*it).s.top().rank_ || (*i).s.top().color==(*it).s.top().color )
{
return true;
}
}
i=i1;
if((*i).s.top().rank_==(*it).s.top().rank_ || (*i).s.top().color==(*it).s.top().color )
return true;
return false;
}
int main()
{
while(cin>>s && s!="#")
{
pilelist.clear();
c.color=s[1];
c.rank_=s[0];
while(!p.s.empty()) p.s.pop();
p.s.push(c);
pilelist.push_back(p);
for(int i=0; i<51; i++)
{
cin>>s;
c.color=s[1];
c.rank_=s[0];
while(!p.s.empty()) p.s.pop();
p.s.push(c);
pilelist.push_back(p);
}
it=pilelist.begin();
while(it!=pilelist.end())
{
i=it;
if(find_equal_card())
{
move_card();
it=pilelist.begin();
}
else
it++;
// cout<<"dklfj"<<endl;
}
it=pilelist.begin();
vector<int> ans;
for(; it!=pilelist.end(); it++)
ans.push_back((*it).s.size());
if(ans.size()==1)
cout<<ans.size()<<" pile remaining: ";
else
cout<<ans.size()<<" piles remaining: ";
int i;
for(i=0; i<ans.size()-1; i++)
cout<<ans[i]<<" ";
cout<<ans[i]<<endl;
}
}
/*
QD AD 8H 5S 3H 5H TC 4D JH KS 6H 8S JS AC AS 8D 2H QS TS 3S AH 4H TH TD 3C 6S
8C 7D 4C 4S 7S 9H 7C 5D 2S KD 2D QH JD 6D 9D JC 2C KH 3D QC 6C 9S KC 7H 9C 5C
AC 2C 3C 4C 5C 6C 7C 8C 9C TC JC QC KC AD 2D 3D 4D 5D 6D 7D 8D TD 9D JD QD KD
AH 2H 3H 4H 5H 6H 7H 8H 9H KH 6S QH TH AS 2S 3S 4S 5S JH 7S 8S 9S TS JS QS KS
#
*/
这道题真心坑,代码中的cout-debug痕迹我没有去除,错误原因是:list是一个循环链表,比如:
#include <iostream>
#include <list>
#include <stack>
using namespace std;
int main()
{
list<int> s;
s.push_back(1);
s.push_back(2);
list<int>::iterator it=s.begin();
it--;
cout<<(*it);
return 0;
}
打印出来的是1
#include <iostream>
#include <list>
#include <stack>
using namespace std;
int main()
{
list<int> s;
s.push_back(1);
s.push_back(2);
list<int>::iterator it=s.begin();
it--;
it--;
cout<<(*it);
return 0;
}
打印出来的是2!
还有pile还有复数要求……