Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Time Limit,不知道如何改进,请高手指教!#include <iostream> #include <string> #include <cctype> using namespace std; class Link{ private: typedef struct node{ string* pos; int count; struct node* next; }Node; Node* head; public: Link(); ~Link(); void Insert(string* pStr); void Display(); }; Link::Link(){ Node* p = new Node; p->pos = new string("000-0000"); p->count = 0; p->next = NULL; head = p; } Link::~Link(){} void Link::Insert(string* pStr){ //如果存在,则count加一;否则按递增方式动态插入 Node* p,* pre,* pNode; p = head->next; pre = head; while(true){ if(p == NULL){ p = new Node; p->pos = pStr; p->count = 1; p->next = NULL; pre->next = p; return; } if((*(p->pos)).compare(*pStr) == 0){ p->count++; return; } if((*(pre->pos)).compare(*pStr)<0 && (*(p->pos)).compare(*pStr) > 0){ pNode = new Node; pNode->next = p; pNode->count = 1; pNode->pos = pStr; pre->next = pNode; return; } else{ pre = p; p = p->next; } } } void Link::Display(){ Node* p; p = head->next; while(p != NULL){ if(p->count > 1) cout<<*(p->pos)<<" "<<p->count<<endl; p = p->next; } } void Format(char* strLine,char* strTele){ int x=0; char table[26] = { '2','2','2', //ABC '3','3','3', //DEF '4','4','4', //GHI '5','5','5', //JKL '6','6','6', //MNO '7','7','7','7', //PQRS '8','8','8', //TUV '9','9','9','9' //WXYZ }; for(int n=0;n<80;n++){ if(x == 3){ strTele[x] = '-'; x++; n--; continue; } if(x == 8) return; if(strLine[n] == '-') continue; if(isdigit(strLine[n])){ strTele[x] = strLine[n]; x++; continue; } if(isalpha(strLine[n])){ strTele[x] = table[strLine[n] - 'A']; x++; } } } void main(){ int lines; char* strLine = new char[80]; char* strTele = new char[9]; string* myStr; Link link; cin>>lines; while(cin.get() != '\n'); for(int n = 0;n<lines;n++){ cin.get(strLine,80,'\n'); while(cin.get() != '\n'); Format(strLine,strTele); strTele[8] = '\0'; myStr = new string(strTele); link.Insert(myStr); } link.Display(); return; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator