WordFilter(vector<string> words) { for (int i = 0; i < words.size(); i++) { t_prefix.insert(words[i], i); reverse(words[i].begin(), words[i].end()); t_suffix.insert(words[i], i); } }
AutocompleteSystem(vector<string> sentences, vector<int> times) { for (int i = 0; i < sentences.size(); i++) { for (int j = 0; j < times[i]; j++) tr.insert(sentences[i]); } node = &tr; }
void makeTrie(void) { map<int, hostConf>::iterator j; string::iterator stringit; char* temp = new char[16]; string prestring; char* size = new char[2]; struct cidrprefix tempFix; for(j = routerEnd.begin(); j != routerEnd.end(); j++) { prestring = (j->second).overlayPrefix; for(stringit = prestring.begin(); stringit != prestring.end(); stringit++) { if(*stringit == '/') { strcpy(temp, prestring.substr(0, distance(prestring.begin(),stringit)).c_str()); cout << "Temp: " << temp << endl; inet_pton(AF_INET, temp, (void *)&tempFix.prefix); stringit++; size[0] = *stringit; stringit++; size[1] = *stringit; break; } } tempFix.size = (char)atoi(size); #ifdef DEBUG cout << "Prefix: " << tempFix.prefix << "/" << (int)tempFix.size << " for router: " << routerIPs[j->first] << endl; #endif hosts.insert(tempFix, routerIPs[j->first]); #ifdef DEBUG cout << "Testing trie." << endl; int testaddr; inet_pton(AF_INET, "1.2.3.1", (void *)&testaddr); cout << testaddr << hosts.search(testaddr) << endl; #endif } }
int main() { while (true) { scanf("%d%d",&n,&m); if (n == 0 && m == 0) break; ac.init(); for (int i = 0; i < n; i++) { scanf("%s",buf); ac.insert(buf,i); } for (int i = 0; i < m; i++) { scanf("%s",buf); ac.insert(buf,-1); } ac.build(); tot = 0; pos[tot++] = 0; for (int i = 0; i < ac.L; i++) if (ac.endv[i] == false && ac.endr[i] != 0) pos[tot++] = i; for (int i = 0; i < tot; i++) Gao(i); memset(dp,-1,sizeof(dp)); dp[0][0] = 0; for (int i = 0; i < (1<<n); i++) for (int j = 0; j < tot; j++) if (dp[i][j] != -1) for (int k = 0; k < tot; k++) if (d[j][k] != -1) { int newsta = i|ac.endr[pos[k]]; if (dp[newsta][k] == -1 || dp[newsta][k] > dp[i][j]+d[j][k]) dp[newsta][k] = dp[i][j]+d[j][k]; } int res = -1; for (int i = 0; i < tot; i++) if (dp[(1<<n)-1][i] != -1) if (res == -1 || res > dp[(1<<n)-1][i]) res = dp[(1<<n)-1][i]; printf("%d\n",res); } return 0; }
/** * Conducts behavior described in "Find Strings" problem */ int main (){ int n,q; cin >> n; //get n if((n < 1) || (n > 50)){ cout << "INVALID"; //1<=n<=50 return 0; } string mystr; for(int i = 0; i < n; i++){ cin >> mystr; //get string if((mystr.length() > 2000) || (mystr.length() < 1)){ cout << "INVALID"; //1<=mi<=2000 return 0; } else{ trie.insert(mystr); } } trie.print(); trie.enum_substrings(substrings); sort(substrings.begin(), substrings.end()); it = unique(substrings.begin(), substrings.end()); substrings.resize(it - substrings.begin()); print_elements(substrings); cin >> q; //get q if((q < 1) || (q > 500)){ cout << "INVALID"; //1<=q<=500 return 0; } else{ int myk; for(int i = 0; i < q; i++){ cin >> myk; //get substring index if(myk > substrings.size() || ((myk > 1000000000) || (myk < 1))){ kstrings.push_back("INVALID"); //1<=k<=1000000000 } else{ kstrings.push_back(substrings[myk - 1]); } } print_elements(kstrings); } return 0; }
int main() { int N, i, j, k; vector<string> init; vector<int> start; string str; char s[100]; while(scanf("%d", &N) != EOF && N) { v.clear(); init.clear(); start.clear(); t.children.clear(); for(i=0;i<m.size();i++) { grafo[i].clear(); peso[i].clear(); tam[i] = INF; } m.clear(); m[""] = 0; for(i=0;i<N;i++) { scanf("%s", s); t.insert(s, 0); v.push_back(s); } for(i=0;i<v.size();i++) { query.clear(); t.match(v[i], 0); for(j=0;j<query.size();j++) if(query[j] != v[i]) { if(!sufix(v[i], query[j])) continue; init.push_back(sufstr); start.push_back(max(v[i].size(), query[j].size())); } } resp = INF; for(i=0;i<init.size();i++) if(start[i] < resp) resp = min(resp, shortest_path(init[i], start[i])); if(resp == INF) printf("-1\n"); else printf("%d\n", resp); } return 0; }
vector<string> input(char c) { vector<string> r; if (c == '#') { tr.insert(s); s.clear(); node = &tr; return r; } s.push_back(c); if (node != nullptr) { node = node->get_next(c); if (node != nullptr) return node->get_top3(); } return r; }
void router(void) { #ifdef DEBUG cout << "I am router #" << hostID << "!" << endl; #endif //bind socket int sockfd = create_cs3516_socket(); //initialize for select() call fd_set readfds; FD_ZERO(&readfds); FD_SET(sockfd, &readfds); //We want a truly nonblocking call so... struct timeval timeoutval; timeoutval.tv_sec = 10; timeoutval.tv_usec = 0; map<string, deque<struct message *> > outputbuffers; map<int, hostIP>::iterator it; struct cidrprefix tempFix; for(it = endIPs.begin(); it != endIPs.end(); it++) { inet_pton(AF_INET, (*it).second.overlay.c_str(), (void *)&tempFix.prefix); tempFix.size = 32; hosts.insert(tempFix, (*it).second.real); outputbuffers[(*it).second.real] = *(new deque<struct message *>); //Try to access the output buffer for this IP, but since it won't exist, create one. #ifdef DEBUG cout << "I just inserted " << hosts.search(tempFix.prefix) << endl; #endif } while(TRUE){ //check if we have anything to read FD_ZERO(&readfds); FD_SET(sockfd, &readfds); select(sockfd+1, &readfds, NULL, NULL, &timeoutval); //while we have a packet to receive, handle it if(FD_ISSET(sockfd, &readfds)){ #ifdef DEBUG cout << "Received a packet!" << endl; #endif struct message *receivemessage = new struct message(); receivemessage->buffer = (char*)malloc(MAX_PACKET_SIZE); cs3516_recv(sockfd, receivemessage->buffer, MAX_PACKET_SIZE); time(&(receivemessage->recvtime)); iphdr *ip = (iphdr*)(receivemessage->buffer); //get the real ip address of the destination string interface = hosts.search((uint32_t)(ip->daddr)); cout << "Going to router: " << interface << endl; deque<struct message *> outputqueue; //Get some info from the packet for logging purposes char srcstr[INET_ADDRSTRLEN], dststr[INET_ADDRSTRLEN]; inet_ntop(AF_INET, &(ip->saddr), srcstr, INET_ADDRSTRLEN); inet_ntop(AF_INET, &(ip->daddr), dststr, INET_ADDRSTRLEN); cout << "This fella is going to " << dststr << endl; short id = ntohs(ip->id); //get the output queue cout << "Interface: " << interface << endl; if(outputbuffers.find(interface)!=outputbuffers.end()){ outputqueue = outputbuffers.find(interface)->second; } else { //we don't know who this is! Log it and drop that packet like it's hot writetolog(srcstr, dststr, id, "NO_ROUTE_TO_HOST", ""); continue; } //decrement the ttl value (ip->ttl)--; if((ip->ttl)>0){ if(outputqueue.size()<=configuration.queueLength){ outputbuffers[interface].push_back(receivemessage); writetolog(srcstr, dststr, id, "SENT_OK", interface); } else { //drop the packet and log writetolog(srcstr, dststr, id, "MAX_SENDQ_EXCEEDED", ""); } } else { //drop the packet and log writetolog(srcstr, dststr, id, "TTL_EXPIRED", ""); } } //look at queues to see if any send delays have elapsed //TODO per-queue send delay as specified by the assignment for(map<string, deque<struct message *> >::iterator i = outputbuffers.begin(); i != outputbuffers.end(); i++) { string interface = (*i).first; double delay = 0; bool isRouter = false; int hostToSend = lookupHost(interface, &isRouter); if(isRouter) delay = routerRouter[hostToSend].sendDelay; else delay = routerEnd[hostToSend].sendDelay; deque<struct message *> buffer = (*i).second; if(buffer.size()>0){ struct message* currentmsg = buffer.front(); time_t currenttime; time(¤ttime); double waited = difftime(currenttime, currentmsg->recvtime); if(waited>(delay/1000)){ unsigned int interfacebytes; //convert address to bytes inet_pton(AF_INET, (char*)interface.c_str(), (void *)&interfacebytes); #ifdef DEBUG cout << "Down to the nitty-gritty: sending the packet." << endl; #endif int status = cs3516_send(sockfd, currentmsg->buffer, MAX_PACKET_SIZE, interfacebytes); //if(status) sent = TRUE; if(!status) fprintf(stderr, "There was an error sending the file. No bytes were sent."); //free(currentmsg->buffer); delete currentmsg; outputbuffers[interface].pop_front(); } } } } }