Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.cpp
- // CodeForces
- //
- // Created by Giorgi Pataraia on 12/29/12.
- //
- #include <iostream>
- #include <cstdio>
- #include <cstdlib>
- #include <string>
- #include <vector>
- #include <set>
- #include <map>
- #include <queue>
- #include <deque>
- #include <algorithm>
- #include <cmath>
- using namespace std;
- #define fori(i,j,n) for (int i = j; i <= n; i++)
- #define ford(i,n,j) for (int i = n; i >= j; i--)
- #define every(t) t.begin(),t.end()
- #define pb(t) push_back(t)
- #define mk(a,b) make_pair(a,b)
- void openFiles() {
- freopen("input.txt", "r",stdin);
- freopen("output.txt","w",stdout);
- }
- const int maxcharcnt = 30;
- void print(string &s,int l,int r,bool withSpace) {
- if (withSpace)
- cout<<" ";
- fori(i,l,r)
- cout<<s[i];
- cout<<endl;
- }
- class node;
- class linkedlist;
- class linkedlist {
- public:
- char curchar;
- node * curnode;
- linkedlist * next;
- linkedlist(char c,node * n) {
- curnode = n;
- next = NULL;
- curchar = c;
- }
- };
- class node {
- public:
- linkedlist * curlist;
- bool isword;
- node () {
- isword = false;
- curlist = NULL;
- }
- };
- node * find(node * head, char c) {
- linkedlist * curlist = head->curlist;
- while(curlist != NULL) {
- if (curlist->curchar == c)
- return curlist->curnode;
- curlist = curlist->next;
- }
- return NULL;
- }
- void add(linkedlist * head,linkedlist * list) {
- if (head->next == NULL) {
- head->next = list;
- }
- else if (head->curchar <= list->curchar && head->next->curchar >= list->curchar) {
- list->next = head->next;
- head->next = list;
- }
- else {
- add(head->next,list);
- }
- }
- void add(node * head,linkedlist * list) {
- linkedlist * curlist = head->curlist;
- if (curlist == NULL) {
- curlist = list;
- }
- else {
- add(curlist,list);
- }
- head->curlist = curlist;
- }
- void insert(node * head,string &s,unsigned int indexInStr) {
- if (indexInStr == s.size()) {
- head->isword = true;
- }
- else {
- node * nextnode = find(head,s[indexInStr]);
- if (nextnode == NULL) {
- linkedlist * list = new linkedlist(s[indexInStr],new node());
- add(head,list);
- nextnode = list->curnode;
- }
- insert(nextnode,s,indexInStr + 1);
- }
- }
- const int maxFindCount = 20;
- int findCount = 0;
- string curstr = "00000000000000000000000000000000000";
- void find(node * head,string &s,unsigned int indexInStr,bool found) {
- if (found) {
- if(head->isword) {
- print(curstr,1,indexInStr-1,true);
- findCount++;
- }
- for (linkedlist * curnode = head->curlist; curnode != NULL; curnode = curnode->next)
- if (findCount < maxFindCount) {
- curstr[indexInStr] = curnode->curchar;
- find(curnode->curnode,s,indexInStr + 1,found);
- }
- }
- else {
- if (indexInStr == s.size()) {
- found = true;
- find(head,s,indexInStr,found);
- }
- else {
- curstr[indexInStr] = s[indexInStr];
- node * curnode = find(head,s[indexInStr]);
- if (curnode != NULL)
- find(curnode,s,indexInStr + 1,found);
- }
- }
- }
- node * root;
- int main() {
- root = new node();
- openFiles();
- string s = "sun";
- insert(root,s,0);
- while(cin>>s) {
- if(s[0] == '?') {
- findCount = 0;
- print(s,1,s.size()-1,false);
- find(root,s,1,false);
- }
- else {
- insert(root,s,1);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement