【例题 6-21 UVA - 506】System Dependencies
生活随笔
收集整理的這篇文章主要介紹了
【例题 6-21 UVA - 506】System Dependencies
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
【鏈接】 我是鏈接,點(diǎn)我呀:)
【題意】
在這里輸入題意
【題解】
記錄每個(gè)物品它的依賴有哪些,以及它被哪些東西依賴就可以了。
顯式安裝的東西不能被隱式刪除刪掉(就是remove item,然后刪除item的依賴的過程叫隱式刪除,而刪除item本身叫顯式刪除);
而只能被顯式刪除。
隱式安裝的依賴則可以被顯式或隱式刪除都行。
(顯示安裝指的是 install item,安裝item本身,而安裝item的依賴,都稱為是隱式的安裝)
寫個(gè)安裝和刪除的遞歸函數(shù)就好。
樣例的答案有誤。
remove browser那里應(yīng)該是先remove tcpip 后remove html.
【代碼】
/*1.Shoud it use long long ?2.Have you ever test several sample(at least therr) yourself?3.Can you promise that the solution is right? At least,the main ideal4.use the puts("") or putchar() or printf and such things?5.init the used array or any value?6.use error MAX_VALUE? */ #include <bits/stdc++.h> using namespace std;string s; map <int,vector <int> > yilai,beiyilai; map <int,int> status; map <string,int> dic; map <int,string> dic2; vector <int> installed; string ope; int tot = 0;void pd(string temp){if (dic[temp]==0) {dic[temp] = ++tot;dic2[tot] = temp;} }void ins(int id,bool highest){if (status[id]==0){if (yilai.find(id)!=yilai.end()){vector <int> v = yilai[id];int len = v.size();for (int i = 0;i < len;i++){int x = v[i];ins(x,0); }}cout << " Installing "<<dic2[id] << endl;installed.push_back(id);status[id] = (highest?1:2);} }bool need(int x){if (beiyilai.find(x)!=beiyilai.end()){vector <int> v = beiyilai[x];int len = v.size();for (int i = 0;i < len;i++){int x = v[i];if (status[x]) return true;}}return false; }void dele(int id,bool highest){if ( !need(id) && (highest || status[id]==2)){status[id] = 0;cout << " Removing " << dic2[id] << endl;installed.erase(remove(installed.begin(),installed.end(),id),installed.end());if (yilai.find(id)!=yilai.end()){vector <int> v = yilai[id];int len = v.size();for (int i = 0;i < len;i++){int x = v[i];if (status[x]) dele(x,0); }}} }int main(){#ifdef LOCAL_DEFINEfreopen("F:\\c++source\\rush_in.txt", "r", stdin);#endifios::sync_with_stdio(0),cin.tie(0);while (getline(cin,s)){yilai.clear(),beiyilai.clear(), status.clear(),dic.clear();dic2.clear();installed.clear();while (s!="END"){cout << s << endl;stringstream ss(s);ss >> ope;if (ope=="INSTALL"){string x;ss >> x;pd(x);int y = dic[x]; if (status[y]!=0)cout <<" "<<x<<" is already installed."<<endl;elseins(y,1);}else if (ope=="REMOVE"){string x;ss >> x;pd(x);int y = dic[x];if (status[y]==0)cout <<" "<<x<<" is not installed."<<endl;else if (need(y))cout <<" "<<x<<" is still needed."<<endl;else{dele(y,1);}}else if (ope=="LIST"){for (int x:installed){cout <<" "<<dic2[x]<<endl;}}else{//dependstring x,y;int xx,yy;ss >> x;pd(x);xx = dic[x];while (ss>>y){pd(y);yy = dic[y];yilai[xx].push_back(yy);beiyilai[yy].push_back(xx);}}getline(cin,s);}cout << s << endl;}return 0; }轉(zhuǎn)載于:https://www.cnblogs.com/AWCXV/p/7857375.html
總結(jié)
以上是生活随笔為你收集整理的【例题 6-21 UVA - 506】System Dependencies的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DDR3布线的那些事儿(二)
- 下一篇: 例行报告11_18