hdu4269 Defend Jan Ge changchun online 恶心模拟
各种恶心。。 直接贴代码了。。。
比较混乱。。 直接用STL或者字符串hash等 会更简洁 更容易....
Problem DescriptionDefend Jian Ge, an interesting RPG map in WAR3, has a very complete equipment system as a mini RPG game.[pre]2ring 1sword 21knife 3: ring 1, sword 11medicine 14+100+ring+sword+knife1shoe 01wing 1: 1medicine 13+10+shoe+wing1shoe 01wing 1: 1medicine 14+10+shoe+wing-wing1shoe 11wing 1: shoe 11medicine 18+100+shoe+shoe+shoe+shoe+shoe+medicine+medicine[/pre]
[pre]Case 1:941knife: 1Case 2:92shoe: 1wing: 1Case 3:101shoe: 1Case 4:946medicine: 1shoe: 1shoe: 1shoe: 1shoe: 1shoe: 1[/pre]
#include<cstdio>#include<algorithm>#include<string>#include<cstring>#include<cmath>#include<map>#include<set>#include<queue>#include<stack>#include<iostream>#define MAX 26#define BUG puts("GF")using namespace std;const bool GF1 = true;int Gold,leftG;char stmp[10000];int N_nme, N_mxe, N_cse;char name_nme[22][30], name_mxe[22][30], name_cse[22][30];int price_nme[22], price_mxe[22], price_cse[22];char mx_part_name[22][60][30];int mx_tt_part[22], mx_part_need[22][60];struct OT{ string os; int ctr; bool operator < (const OT &a) const{ return os<a.os; }};set<OT> Set;map<string,int> type;map<string,int> priceCSE;map<string,int> priceNME;map<string,int> priceMXE;map<string,int> MXEID;typedef struct TrieNode { int nCount; struct TrieNode *next[MAX];}TrieNode;TrieNode Memory[100000];int allocp =0;TrieNode *CreateTrieNode() { int i; TrieNode *p; p = &Memory[allocp++]; p->nCount = 0; for(i =0 ; i < MAX ; i++) { p->next[i] = NULL; } return p;}void InsertTrie(TrieNode * &pRoot , char*s) { int i, k; TrieNode *p; if(!(p = pRoot)) { p = pRoot = CreateTrieNode(); } i = 0; while(s[i]) { k = s[i++] - 'a'; if( ! p->next[k]) p->next[k] = CreateTrieNode(); p = p->next[k]; } p->nCount += 1;}void DeletTrie(TrieNode * &pRoot, char *s, int dn){ TrieNode *p; int i , k; if(!(p = pRoot)) { return ; } i = 0; while(s[i]) { k = s[i++] -'a'; if(p->next[k] == NULL) return ; p = p->next[k]; } p->nCount -= dn; return ;}int SearchTrie(TrieNode * &pRoot , char*s) { TrieNode *p; int i , k; if(!(p = pRoot)) { return 0; } i = 0; while(s[i]) { k = s[i++] -'a'; if(p->next[k] == NULL) return 0; p = p->next[k]; } return p->nCount;}TrieNode * box = NULL;void init(){ box = NULL; allocp = 0; Gold = 0; leftG = 6; //N_nme = N_mxe = N_cse = 0; type.clear(); priceNME.clear(); priceCSE.clear(); priceMXE.clear(); MXEID.clear(); Set.clear(); memset(mx_tt_part,0,sizeof(mx_tt_part)); memset(name_nme,0,sizeof(name_nme)); memset(name_mxe,0,sizeof(name_mxe)); memset(name_cse,0,sizeof(name_cse)); memset(price_nme,0,sizeof(price_nme)); memset(price_mxe,0,sizeof(price_mxe)); memset(price_cse,0,sizeof(price_cse)); memset(mx_part_name,0,sizeof(mx_part_name)); memset(mx_part_need,0,sizeof(mx_part_need));}void input(){ getchar(); for(int i=0; i<N_nme; ++i) { scanf("%s %d",name_nme[i],&price_nme[i]); string nnnn1; nnnn1 = (name_nme[i]); type[nnnn1] = 1; priceNME[nnnn1] = price_nme[i]; } scanf("%d",&N_mxe); getchar(); for(int i=0; i<N_mxe; ++i) { gets(stmp); int len = strlen(stmp); int cur = 0; sscanf(stmp,"%s%d",name_mxe[i],price_mxe+i); string sk(name_mxe[i]); priceMXE[sk] = price_mxe[i]; MXEID[sk] = i; type[sk] = 2; while( (*(stmp+cur)) != ':') ++cur; for(++cur;cur<len;++cur) { sscanf(stmp+cur,"%s%d",mx_part_name[i][ mx_tt_part[i] ],&mx_part_need[i][ mx_tt_part[i] ]); mx_tt_part[i] ++; while(cur < len && (*(stmp+cur)) != ',') ++cur; ++cur; } } scanf("%d",&N_cse); getchar(); for(int i=0; i<N_cse;++i) { scanf("%s%d",name_cse+i, price_cse+i); string nnnn3(name_cse[i]); type[nnnn3] = 3; priceCSE[nnnn3] = price_cse[i]; } }void obtain(char * name){ string mm(name); int swich2 = type[mm]; if(swich2 == 1 && Gold >= priceNME[mm] && leftG >0) { Gold -= priceNME[mm]; leftG -= 1; InsertTrie(box,name); return ; } if(swich2 == 3 && Gold >= priceCSE[mm] && leftG >0) { int ex = SearchTrie(box,name); if( ex == 0 ) { if(leftG >=1) { leftG -= 1; Gold -= priceCSE[mm]; InsertTrie(box,name); } else return ; } else if (ex != 0) { Gold -= priceCSE[mm]; InsertTrie(box,name); } } if(swich2 == 2 && Gold >= price_mxe[MXEID[mm]]) { int ffg = 1; for(int i=0; i<mx_tt_part[ MXEID[mm] ];++i) { if( SearchTrie(box,mx_part_name[MXEID[mm]][i]) < mx_part_need[MXEID[mm]][i]) { ffg = 0; break; } } if(ffg) { InsertTrie(box,name); Gold -= price_mxe[MXEID[mm]]; leftG -= 1; for(int i=0; i<mx_tt_part[ MXEID[mm] ];++i) { if(mx_part_need[MXEID[mm]][i] == 0) continue ; string jj(mx_part_name[ MXEID[mm] ][i]); int swch = type[jj]; DeletTrie(box,mx_part_name[MXEID[mm]][i],mx_part_need[MXEID[mm]][i]); if( swch == 1 || swch == 2) { leftG += mx_part_need[MXEID[mm]][i]; } if( swch == 3) { if(SearchTrie(box,mx_part_name[MXEID[mm]][i]) == mx_part_need[MXEID[mm]][i]) { leftG += 1; } } } } return ; }}void sold(char * name){ int num9 = SearchTrie(box, name); if( num9 == 0) return ; leftG += 1; string etmp(name); int swich = type[etmp]; if(swich == 3) { Gold += num9 * priceCSE[etmp]; DeletTrie(box,name,num9); return ; } if(swich == 1) { Gold += priceNME[etmp]; DeletTrie(box,name,1); return ; } if(swich == 2) { DeletTrie(box,name,1); stack<string> Stak; Stak.push(etmp); string fobl; while(! Stak.empty()) { etmp = Stak.top(); Stak.pop(); Gold += priceMXE[etmp]; for(int i=0; i<mx_tt_part[ MXEID[etmp] ]; ++i) { fobl = mx_part_name[MXEID[etmp]][i]; if(type[ fobl ] == 2) Stak.push(fobl); else if(type[fobl] == 1) Gold+= priceNME[fobl]*mx_part_need[MXEID[etmp]][i]; else if(type[fobl] == 3) Gold+= priceCSE[fobl]*mx_part_need[MXEID[etmp]][i]; } } return; }}void operate(){ char flag = 0; getchar(); char op = getchar(); char reds[30]; int getgold = -999; scanf("%s",reds); if(reds[0]>='0' && reds[0]<='9') { sscanf(reds,"%d",&getgold); } if(op == '+' && getgold >=0) { Gold += getgold; return; } if(op == '+' && getgold < 0) { obtain(reds); return ; } if(op == '-' && getgold <0) { sold(reds); return ; } return;}int main(){ int CSN = 1; while(scanf("%d",&N_nme) != EOF) { int M; init(); input(); scanf("%d",&M); for(int i = 1; i <= M; ++i) { operate(); } getchar(); cout<<"Case "<<CSN++<<':'<<endl; cout<<Gold<<endl; cout<<6-leftG<<endl; for(int i=0;i<N_nme;++i) { int sp; if(sp = SearchTrie(box,name_nme[i])) { OT foobar; foobar.os = name_nme[i]; foobar.ctr = sp; Set.insert(foobar); } } for(int i=0;i<N_mxe;++i) { int sp1; if(sp1 = SearchTrie(box,name_mxe[i])) { OT foobar; foobar.os = name_mxe[i]; foobar.ctr = sp1; Set.insert(foobar); } } for(int i=0;i<N_cse;++i) { int sp; if(sp = SearchTrie(box,name_cse[i])) { OT foobar; foobar.os = name_cse[i]; foobar.ctr = sp; Set.insert(foobar); } } set<OT>::iterator it = Set.begin(); for(;it != Set.end();++it) { if(type[(*it).os] == 3) cout<< (*it).os<<": "<<(*it).ctr<<endl; else { for(int i=0;i<(*it).ctr;++i) cout<< (*it).os<<": "<<1<<endl; } } cout<<endl; } return 0;}