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 |
我的代码,为什么总是wrong answer呢?我用Trie树做的,题目测试数据和其他帖子给出的测试数据都过了,还是wa,输入输出也改了很多部分,为什么就不行呢? #include <iostream> using namespace std; const int LenPerLine = 200; char arr[LenPerLine]; int score = 0; int curNum = 0; bool isMinus = false; const int num_chars = 26; const int NON = -2; int curTime = 0; struct Trie_node { int data; Trie_node * branch[num_chars]; Trie_node() { data = NON; for(int i = 0; i < num_chars; ++i) { branch[i] = NULL; } } }; class Trie { public: Trie() { root = new Trie_node(); } ~Trie() { //TODO: delete nodes Trie_node * tmpPtr = root; delete root; } int search(const char* word, int & entry) const; int insert(const char* word, int entry); //int remove(const char* word, char * entry); protected: Trie_node * root; }; int Trie::search(const char * word, int &entry) const { int result = 1; char cur_Char; Trie_node * loc = root; while(loc != NULL && *word != '\0') { if(*word >= 'a' && *word <= 'z') { cur_Char = *word - 'a'; } else { return 0; } loc = loc->branch[cur_Char]; word++; } if(loc != NULL && loc->data != NON) { // entry = new char[strlen(loc->data)]; // memcpy(entry, loc->data); entry = loc->data; } else result = 0; return result; } int Trie::insert(const char * word, int entry) { int result = 1; Trie_node * loc = root; char cur_Char; while(loc != NULL && *word != '\0') { //don't assign //curChar = *word; if(*word >= 'a' && *word <= 'z') { cur_Char = *word - 'a'; } else { return 0; } if(loc->branch[cur_Char] == NULL) loc->branch[cur_Char] = new Trie_node(); loc = loc->branch[cur_Char]; word++; } if(loc != NULL && loc->data != NON) { result = 0; } else { //loc->data = new char[strlen(entry) + 1]; //memcpy(loc->data, entry); loc->data = entry; } return result; } Trie t; void initTrie() { t.insert("one", 1); t.insert("two", 2); t.insert("three", 3); t.insert("four", 4); t.insert("five", 5); t.insert("six", 6); t.insert("seven", 7); t.insert("eight", 8); t.insert("nine", 9); t.insert("ten", 10); t.insert("eleven", 11); t.insert("twelve", 12); t.insert("thirteen", 13); t.insert("fourteen", 14); t.insert("fifteen", 15); t.insert("sixteen", 16); t.insert("seventeen", 17); t.insert("eighteen", 18); t.insert("nineteen", 19); t.insert("twenty", 20); t.insert("thirty", 30); t.insert("forty", 40); t.insert("fifty", 50); t.insert("sixty", 60); t.insert("seventy", 70); t.insert("eighty", 80); t.insert("ninety", 90); t.insert("hundred", 100); t.insert("thousand", 1000); t.insert("million", 1000000); t.insert("zero", 0); t.insert("negative", -1); } void process(char *p) { int re; char buffer[LenPerLine]; int i = 0; isMinus = false; curTime = 0; while(true) { if(*p != ' ' && *p != '\0') { buffer[i++] = *p; } else { //outputValue buffer[i] = '\0'; i = 0; t.search(buffer, re); if(re >= 100) { //end way if(curTime && curTime < re) { score += curNum; score *= re; curNum = 0; curTime = re; } else { score += curNum * re; curNum = 0; curTime = re; } } else if(re >= 0) { //mul way curNum += re; } else { //flag isMinus = true; } } if(*p != '\0') p++; else break; } if(curNum != 0) score += curNum; score = isMinus?-score:score; cout<<score<<'\n'; } int main() { initTrie(); while(cin.getline(arr, LenPerLine)) { if(*arr == '\0') break; score = 0; curNum = 0; process(arr); } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator