標籤:
嗯……這道題大思路很明顯,但是細節好煩人……
大體上就是stack+set
對於棧中的元素,可以發現每個元素都是一個集合(set),而集合中的元素也是集合
因此,應該對每個集合(元素)進行編號 typedef set<int> element ,這樣就能把每個元素看作儲存整數的棧 stack s;
用map和vector進行映射集合(元素)和編號
map<element,int> ID_eache;vector<int> ID_eache2;
然後再按照要求寫就行了
其中可用switch case判斷操作。
要注意每個case都需要 break;
因為case只是入口,並不是出口。只要進去後,沒有break就會一直運行下去
1 #include <cstdio> 2 #include <set> 3 #include <stack> 4 #include <iostream> 5 #include <map> 6 #include <vector> 7 using namespace std; 8 9 class LOVE{10 private:11 int n;12 typedef set<int> element;13 stack <int> s;14 15 map<element,int> ID_cache;16 vector<element> ID_cache2;17 18 int ID(element x){19 if(ID_cache.count(x))return ID_cache[x];20 ID_cache2.push_back(x);21 return ID_cache[x]=ID_cache2.size()-1;22 }23 24 public:25 void start(){26 scanf("%d",&n);27 char com[10];28 while(n--){29 scanf("%s",com);30 element temp1,temp2,temp3;31 element::iterator it1,it2,it;32 switch(com[0]){33 case ‘P‘:34 s.push(ID(element()));35 break;36 case ‘D‘:37 s.push(s.top());38 break;39 case ‘U‘:40 temp1=ID_cache2[s.top()];41 s.pop();42 temp2=ID_cache2[s.top()];43 s.pop();44 for(it=temp2.begin();it!=temp2.end();it++)45 temp1.insert(*it);46 s.push(ID(temp1));47 break;48 case ‘I‘:49 temp1=ID_cache2[s.top()];50 s.pop();51 temp2=ID_cache2[s.top()];52 s.pop();53 for(it1=temp1.begin();it1!=temp1.end();it1++){54 for(it2=temp2.begin();it2!=temp2.end();it2++){55 if(*it1==*it2){56 temp3.insert(*it1);57 temp2.erase(*it2);58 break;59 }60 }61 }62 s.push(ID(temp3));63 break;64 case ‘A‘:65 temp1=ID_cache2[s.top()];66 s.pop();67 temp2=ID_cache2[s.top()];68 s.pop();69 temp2.insert(ID(temp1));70 s.push(ID(temp2));71 break;72 }73 cout<<ID_cache2[s.top()].size()<<endl;74 }75 }76 };77 78 79 int main(){80 //freopen("in.txt","r",stdin);81 int n;82 scanf("%d",&n);83 while(n--){84 LOVE LIVE;85 LIVE.start();86 printf("***\n");87 }88 return 0;89 }
Uva 12096.The SetStack Computer