http://tioj.ck.tp.edu.tw/problems/1302
心血來潮,第一次實作HashTable,可支援插入、刪除和查詢,這題還可以用其他方法,不解釋(STL好像有一個東西叫map的樣子……)。
http://tioj.ck.tp.edu.tw/problems/1302
心血來潮,第一次實作HashTable,可支援插入、刪除和查詢,這題還可以用其他方法,不解釋(STL好像有一個東西叫map的樣子……)。
好久沒玩指標和鏈結串列了,對它有點生疏,而且我發現每次寫的時候,我都要再仔細的完全理解一遍,這就當作複習吧!!!
#include <iostream> #include <stdio.h> #include <string.h> using namespace std; struct Node { string word; Node *next; }; inline bool is_number(char ch) { return '0' <= ch && ch <= '9'; } inline bool is_alphabet(char ch) { return ('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z'); } int main() { Node *head = NULL; string word; char ch; ch = getchar(); while(true) { if(is_number(ch)) { if(ch == '0') break; int num = 0; while(is_number(ch)) { num = num * 10 + (ch - '0'); ch = getchar(); } Node *now = head, *prev = NULL; for(int i = 1; i < num ; i ++) { prev = now; now = now->next; } cout << now->word; if(now != head) { prev->next = now->next; now->next = head; head = now; } } else if(is_alphabet(ch)) { string word; while(is_alphabet(ch)) { word += ch; ch = getchar(); } cout << word; Node *n = new Node; n->word = word; n->next = head; head = n; } else { putchar(ch); ch = getchar(); } } return 0; }