UVA 11239

来源:互联网 发布:linux的snmp 详解 编辑:程序博客网 时间:2024/06/06 02:17

Thinking: Here we used the structure "map" to achieve our goal. Quite easy actually :)

Remember to clear the map in next case.


AC code:

#include<iostream>#include<cstring>#include<cmath>#include<map>#include<algorithm>#include<vector>#include<stack>#include<queue>#include<string>#define MAX(a,b) ((a)>(b)?(a):(b))#define MIN(a,b) ((a)<(b)?(a):(b))using namespace std;struct pro{char name[105];map<string, int> student;};pro project[105]; bool cmp(const pro &a, const pro &b){if (a.student.size() == b.student.size())return strcmp(a.name, b.name) < 0;else return a.student.size() > b.student.size();/*if (a.student.size() > b.student.size())return true;else if (a.student.size() < b.student.size())return false;else return a.name < b.name;*/}int main(){//freopen("in.txt", "r", stdin);//freopen("out.txt", "w", stdout);char input[100];char temp[100];bool lastone = false;while (!lastone){map<string, bool> abandon;int count_pro = 0;while (gets(input)){if (input[0] == '1')//deal with data got till now{sort(project + 1, project + 1 + count_pro, cmp);for (int i = 1; i <= count_pro; i++)printf("%s %d\n", project[i].name, project[i].student.size());break;}if (input[0] == '0'){lastone = true;break;}if (input[0] >= 'A'&&input[0] <= 'Z')//input is project{project[++count_pro].student.clear();strcpy(project[count_pro].name, input);continue;}else{if (abandon.find(input) == abandon.end()){if (project[count_pro].student.find(input) == project[count_pro].student.end())//this student is not in  this before{for (int i = 1; i < count_pro; i++){if (project[i].student.find(input) != project[i].student.end()){project[i].student.erase(input);abandon[input] = true;}}if (abandon.find(input) == abandon.end())project[count_pro].student[input] = 1;}}}}}return 0;}

1 0
原创粉丝点击