Untitled
plain_text
2 months ago
4.3 kB
3
Indexable
Never
----------------------MAIN------------------------------ #ifndef _CRT_SECURE_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS #endif #include <stdio.h> #define INPUTWORD (100) #define RECOMMEND (200) #define BANWORD (300) extern void init(); extern void inputWord(char mWord[20]); extern int recommend(char mUser[20], char mAnswer[20]); extern void banWord(char mWord[20]); static int mstrcmp(const char str1[], const char str2[]) { int c = 0; while (str1[c] != 0 && str1[c] == str2[c]) ++c; return str1[c] - str2[c]; } static int mstrlen(const char str[]) { int ret = 0; while (str[ret]) ++ret; return ret; } static int run() { int ret_val = 1; int type, len; char mWord[20] = {}, mAnswer[20] = {}, ans[20] = {}; init(); int N; scanf("%d", &N); for (int i = 0; i < N; ++i) { scanf("%d %s", &type, mWord); switch (type) { case INPUTWORD: inputWord(mWord); break; case RECOMMEND: len = recommend(mWord, mAnswer); scanf("%s", ans); if (len != mstrlen(ans)) ret_val = 0; if (mstrcmp(ans, mAnswer) != 0) ret_val = 0; break; case BANWORD: banWord(mWord); break; default: ret_val = 0; break; } } return ret_val; } int main() { setbuf(stdout, NULL); freopen("input.txt", "r", stdin); int T, MARK; scanf("%d %d", &T, &MARK); for (int tc = 1; tc <= T; tc++) { int score = run() ? MARK : 0; printf("#%d %d\n", tc, score); } return 0; } --------------------------USER---------------------------- #include <string> #inc using namespace std; void mstrcpy(char dst[], const char src[]) { int c = 0; while ((dst[c] = src[c]) != 0) ++c; } int mstrlen(const char str[]) { int ret = 0; while (str[ret]) ++ret; return ret; } int mstrcmp(const char str1[], const char str2[]) { int c = 0; while (str1[c] != 0 && str1[c] == str2[c]) ++c; return str1[c] - str2[c]; } struct Node{ int id; int cnt; bool isEndOfWord; Node* child[26]; }; Node* createNewNode(){ Node* node = new Node(); node ->cnt = 0; for(int i = 0; i < 26;i++){ node->child[i] = NULL; } node->isEndOfWord = false; return node; } Node* root; int index; void init() { root = createNewNode(); index = 1; } void inputWord(char mWord[20]) { Node* ptr = root; int idx; for(int i = 0; i < mstrlen(mWord); i++){ idx = mWord[i] - 'a'; if(ptr->child[idx] == NULL){ ptr->child[idx] = createNewNode(); } ptr = ptr->child[idx]; ptr->cnt += 1; ptr->id = index; } ptr->isEndOfWord = true; index++; } int recommend(char mUser[20], char mAnswer[20]) { Node* ptr = root; int idx; int length = 0; for(int i = 0; i < mstrlen(mUser); i++){ int index = mUser[i]- 'a'; if(ptr->child[idx] == NULL){ return ++length; } ptr = ptr->child[idx]; length++; } while(1){ } return length; } void banWord(char mWord[20]) { } -------------------------------TRIE---------------------------- #include <iostream> #include <string> using namespace std; struct Node{ int cnt; bool isEndOfWord; Node* child[26]; }; Node* root; Node* createNewNode(){ Node* node = new Node(); node ->cnt = 0; for(int i = 0; i < 26;i++){ node->child[i] = NULL; } node->isEndOfWord = false; return node; } void insert(string s) { Node* ptr = root; for(int i = 0; i < s.length(); i++){ int index = s[i]- 'a'; if(ptr->child[index] == NULL){ ptr->child[index] = createNewNode(); } ptr = ptr->child[index]; ptr->cnt += 1; } ptr->isEndOfWord = true; } void erase(string s) { Node* ptr = root; for(int i = 0; i < s.length(); i++){ int index = s[i]- 'a'; if(ptr->child[index] == NULL){ return; } ptr = ptr->child[index]; } if(ptr->isEndOfWord){ ptr = root; for(int i = 0; i < s.length(); i++){ int index = s[i]- 'a'; ptr = ptr->child[index]; ptr->cnt -= 1; } ptr->isEndOfWord = false; } } int getCount(string s) //dem co bao nhieu thang bat dau bang xau s { Node* ptr = root; for(int i = 0; i < s.length(); i++){ int index = s[i]- 'a'; if(ptr->child[index] == NULL){ return 0; } ptr = ptr->child[index]; } return ptr->cnt; } int main() { root = createNewNode(); insert("abc"); insert("cdf"); insert("abd"); cout << getCount("ab") << endl; erase("abd"); cout << getCount("ab") << endl; return 0; }