Exemplo n.º 1
0
void read_trackData()
{
	FILE *fp = fopen("trackData1.txt","r");
	int value;
	char *cur;
	char buf[200];
	int trackid, albumid, artistid;
	while(! feof(fp) ) {
		fgets(buf, 200, fp);
		if (feof(fp)) break;

		trackid = atoi(strtok(buf, "|"));

		if(imap.count(trackid) == 0 )
			continue;
		int iid = imap[trackid];
		items[iid].type = TRACK;
		cur = strtok(NULL, "|");
		if (cur[0] != 'N') {
			albumid = atoi(cur);
			items[iid].albumid = albumMap[albumid];
		}
		cur = strtok(NULL, "|");
		if (cur[0] != 'N') {
			artistid = atoi(cur);
			items[iid].artistid = artistMap[artistid];
		}
		while( (cur = strtok(NULL, "|")) != NULL) {
			int value = atoi(cur);
			int gid = genreMap[value];
			genreItemMap.insert(hash_multimap<int,int>::value_type(iid, gid));
		}
	}
}
Exemplo n.º 2
0
void read_albumData()
{
	FILE *fp = fopen("albumData1.txt","r");
	int value;
	char *cur;
	char buf[200];
	int trackid, albumid, artistid;
	int aid = 0;
	while(! feof(fp) ) {
		fgets(buf, 200, fp);
		if (feof(fp)) break;

		albumid = atoi(strtok(buf, "|"));
		albumMap[albumid] = aid;

		if (imap.count(albumid) != 0) {
			int iid = imap[albumid];
			cur = strtok(NULL, "|");

			items[iid].albumid = aid;
			if (cur[0] != 'N') {
				items[iid].artistid = artistMap[atoi(cur)];
			}
			while( (cur = strtok(NULL, "|")) != NULL) {
				int value = atoi(cur);
				int gid = genreMap[value];
				genreItemMap.insert(hash_multimap<int,int>::value_type(iid, gid));
			}
		}
		aid++;
	}
}
Exemplo n.º 3
0
int preOrder(Node* head, int sum, int preSum, int level, int &maxlen, hash_map<int, int> &sumMap)
{
	if (head == NULL)
		return maxlen;
	int curSum = preSum + head->value;
	if (sumMap.count(curSum - sum) == 1)
	{
		maxlen = (level - sumMap[curSum - sum]) > maxlen ? (level - sumMap[curSum - sum]) : maxlen;
	}
	if (sumMap.count(curSum) == 0)
	{
		sumMap.insert(make_pair(curSum, level));
	}
	maxlen = preOrder(head->left, sum, curSum, level + 1, maxlen, sumMap);
	maxlen = preOrder(head->right, sum, curSum, level + 1, maxlen, sumMap);
	if (level == sumMap[curSum])
	{
		sumMap.erase(curSum);
	}
	return maxlen;
}
Exemplo n.º 4
0
void read_validate()
{
	int uid, n, iid, stmp, day, hr, min, sec;
	double  score;
	ssize_t didx = 0;
	unsigned int uidx=0, ridx=0, iidx=0;
	ssize_t size;
	char *data = (char *)open_read("validationIdx1.txt", &size);
	int sum = 0;
	while(1) {
		if ( parseInt(data, &didx, &uid, size) ) {
			break;
		}
		uidx = umap[uid];
		parseInt(data, &didx, &n, size);
		//users[uidx].nu = n;
		for(int i = 0; i < n; i++) {
			parseInt(data, &didx, &iid, size);
			parseInt(data, &didx, &stmp, size);
			parseInt(data, &didx, &day, size);
			parseInt(data, &didx, &hr, size);
			parseInt(data, &didx, &min, size);
			parseInt(data, &didx, &sec, size);
			score =  (double)stmp / SCORENORM;
			day -= 2672;
			if( imap.count(iid) == 0) {
				iidx = imaplen;
				imaplen ++;
				imap[iid] = iidx;

				items[iidx].id = iid;
				items[iidx].count = 0;
				items[iidx].artistid = -1;
				items[iidx].albumid = -1;
			} else {
				iidx = imap[iid];
				assert(items[iidx].id == iid);
			}
			validations[ridx].item = iidx;
			validations[ridx].rating = score;
			validations[ridx].day = day;
			ridx++;
		}
	}
	munmap(data, size);
	nValidations = ridx;
	printf("nValidations = %d\n", nValidations);
}
Exemplo n.º 5
0
void read_tests()
{
	int uid, n, iid, stmp, day, hr, min, sec;
	double  score;
	ssize_t didx = 0;
	unsigned int uidx=0, ridx=0, iidx=0;
	ssize_t size;
	char *data = (char *)open_read("testIdx1.txt", &size);
	int sum = 0;
	while(1) {
		if ( parseInt(data, &didx, &uid, size) ) {
			break;
		}
		uidx = umap[uid];
		parseInt(data, &didx, &n, size);
		if (n != 6) printf("Error size not 6!=%d\n", n);
		//users[uidx].qu = n;
		for(int i =0; i < n; i++) {
			parseInt(data, &didx, &iid, size);
			parseInt(data, &didx, &day, size);
			parseInt(data, &didx, &hr, size);
			parseInt(data, &didx, &min, size);
			parseInt(data, &didx, &sec, size);
			day -= 2672;
			if( imap.count(iid) == 0) {
				iidx = imaplen;
				imaplen ++;
				imap[iid] = iidx;

				items[iidx].id = iid;
				items[iidx].count = 0;
				items[iidx].albumid = -1;
				items[iidx].artistid = -1;
			} else {
				iidx = imap[iid];
				assert(items[iidx].id == iid);
			}
			tests[ridx].item = iidx;
			tests[ridx].rating = 0;
			tests[ridx].day = day;
			ridx++;
		}
	}
	munmap(data, size);
	nTests = ridx;
	printf("nTests = %d %d\n", nTests, ridx);
}
Exemplo n.º 6
0
void read_genres()
{
	FILE *fp = fopen("genreData1.txt","r");
	int lines = 0, value;
	int gid = 0;
	while(! feof(fp) ) {
		fscanf(fp, "%d\n", &value);
		if(feof(fp)) break;
		genreMap[value] = gid;
		if(imap.count(value) > 0) {
			int iid = imap[value];
			items[iid].type = GENRE;
			genreItemMap.insert(hash_multimap<int,int>::value_type(iid, gid));
		}
		gid++;
	}
}
Exemplo n.º 7
0
bool check_for_repetitions(sol s,int iter,int n) {
    visita v,nova;
    int lenght;
    steps_since_last_change++;
    if(visitas.count(s.custo)) {
        //printf("jah visitei\n");
        v = visitas[s.custo];
        visitas[s.custo].freq++;
        lenght = iter - v.iter;
        visitas[s.custo].iter = iter;
        //printf("freq : %d\n",v.freq+1);
        if( v.freq+1 > REP) {
            chaotic++;
            if(chaotic > MAXCHAOS) {
                chaotic = 0;
                //visitas.clear();
                return false;
            }
        }
        //if(lenght < cicle_max){
        moving_average = 0.05*lenght + 0.95*moving_average;
        list_size = Mult(list_size,INCREASE);
        steps_since_last_change = 0;
        //}
        if(list_size >= n*n) {
            list_size = Mult(list_size,DECREASE/2.0);
            list_size = ( list_size < 2 ) ? 2 : list_size;
        }
    }
    else {
        nova.custo = s.custo;
        nova.iter = iter;
        nova.freq = 1;
        visitas[s.custo] = nova;
    }
    //printf("moving_average : %.3lf, steps_since_last_change: %d\n",moving_average,steps_since_last_change);

    if(steps_since_last_change > MAX_STEPS) {
        //printf("list_size : %d, dec : %.3lf\n",list_size,dec);
        list_size = Mult(list_size,DECREASE);
        list_size = ( list_size < 2 ) ? 2 : list_size;
        steps_since_last_change = 0;
    }
    return true;
}
Exemplo n.º 8
0
void read_artists()
{
	FILE *fp = fopen("artistData1.txt","r");
	int lines = 0, value;
	int aid = 0;
	while(! feof(fp) ) {
		fscanf(fp, "%d\n", &value);
		if(feof(fp)) break;
		artistMap[value] = aid;
		if(imap.count(value) != 0) {
			int iid = imap[value];
			items[iid].type = ARTIST;
			items[iid].artistid = aid;
			items[iid].albumid = -1;
		}
		aid++;
	}
}
Exemplo n.º 9
0
int main() {
  int T, n;
  scanf("%d", &T);
  while(T--) {
    scanf("%d", &n);
    cur.clear();
    for(int i = 0; i < n; i++) {
      scanf("%d", &A[i]);
      if(!cur.count(A[i])) last[i] = -1;
      else last[i] = cur[A[i]];
      cur[A[i]] = i;
    }

    int L = 0, R = 0, ans = 0;
    while(R < n) {
      while(R < n && last[R] < L) R++;
      ans = max(ans, R - L);
      L++;
    }
    printf("%d\n", ans);
  }
  return 0;
}
Exemplo n.º 10
0
int create_stack_boxes(vector<box> boxes, box bottom, vector<box> & stack, hash_map stack_map) {
    if(bottom != NULL && stack_map.count(bottom) > 0) {
        stack = stack_map[bottom];
    }
    
    int max_height = 0;
    vector<box> max_stack;
    
    for(int i = 0; i < boxes.size(); i++) {
        if(canbeabove(boxes[i], bottom)) {
            vector<box> new_stack;
            int stack_height = create_stack_boxes(boxes, boxes[i], new_stack, stack_map);
            if(stack_height > max_height) {
                max_height = stack_height;
                max_stack = new_stack;
            }
        }
    }
    
    if(bottom != NULL) {
        max_stack[0] = bottom;
    }
    stack_map[bottom] = max_stack;
}
Exemplo n.º 11
0
bool request_handler::url_parse(const std::string& url,
                                std::string& request_path,
                                hash_map< std::string, std::vector<std::string> >& query_params)
{
    std::string::size_type path_len;
    try
    {
        path_len = url.find_first_of("?");
    }
    catch (std::exception& e)
    {
        path_len = url.size();
    }

    try
    {
        request_path = libtorrent::unescape_string(url.substr(0, path_len));
    }
    catch (std::runtime_error& exc)
    {
        return false;
    }

    std::string query_string = url.substr(path_len + 1, url.size() - (path_len + 1));

    str query_copy = strdup(query_string.c_str());
    boost::shared_ptr<char> fancy(query_copy, free);
    str query = query_copy;
    str kvpairs = strsep(&query, '&');
    for (; kvpairs != NULL; kvpairs = strsep(&query, '&')) {
        if (*kvpairs == 0)
            continue;
        
        str kvpairs2 = strdup(kvpairs);
        boost::shared_ptr<char> fancy(kvpairs2, free);
        
        str skey = strsep(&kvpairs2, '=');
        if (skey == NULL || *skey == 0)
            continue;
        str sval = strsep(&kvpairs2, '=');
        if (sval == NULL || *sval == 0)
            continue;

        std::string key;
        std::string val;

        try
        {
            key = libtorrent::unescape_string(skey);
        }
        catch (std::runtime_error& exc)
        {
            return false;
        }

        try
        {
            val = libtorrent::unescape_string(sval);
        }
        catch (std::runtime_error& exc)
        {
            return false;
        }

        //logger << key << " = " << val << std::endl;
        if (query_params.count(key))
        {
            std::vector<std::string>& vals = query_params[key];
            vals.push_back(val);
        }
        else
        {
            std::vector<std::string> vals;
            vals.push_back(val);
            query_params[key] = vals;
        }
    }

    return true;
}
Exemplo n.º 12
0
void read_data()
{
	int uid, n, iid, stmp, day, hr, min, sec;
	double  score;
	ssize_t didx = 0;
	unsigned int uidx=0, ridx=0, iidx=0;
	ssize_t size;
	char *data = (char *)open_read("trainIdx1.txt", &size);
	int sum = 0;
	int start = 0;
	mu = 0;
	while(1) {
		if ( parseInt(data, &didx, &uid, size) ) {
			break;
		}
		//printf("' %c\n", data[didx-1]);
		if (umap.count(uid) == 0) {
			uidx = umaplen;
			umaplen++;
			umap[uid] = uidx;
			users[uidx].id = uid;
		} else {
			uidx = imap[uid];
		}
		parseInt(data, &didx, &n, size);
		users[uidx].count = n;
		users[uidx].start = start;
		start += n;
		for(int i =0; i < n; i++) {
			parseInt(data, &didx, &iid, size);
			parseInt(data, &didx, &stmp, size);
			parseInt(data, &didx, &day, size);
			parseInt(data, &didx, &hr, size);
			parseInt(data, &didx, &min, size);
			parseInt(data, &didx, &sec, size);
			score = (double) stmp / SCORENORM;
			day -= 2672;
			mu += score;
			if( imap.count(iid) == 0) {
				iidx = imaplen;
				imaplen ++;
				imap[iid] = iidx;

				items[iidx].id = iid;
				items[iidx].count = 0;
				items[iidx].artistid = -1;
				items[iidx].albumid = -1;
			} else {
				iidx = imap[iid];
				assert(items[iidx].id == iid);
			}
			items[iidx].count += 1;
			ratings[ridx].item = iidx;
			ratings[ridx].rating = score;
			ratings[ridx].day = day;
			assert(ratings[ridx].rating <= 100);
			ridx++;
		}
	}
	munmap(data, size);
	read_validate();
	read_tests();
	nItems = imaplen;
	nUsers = umaplen;
	nRatings = ridx;
	mu /= (double)ridx;
	printf("nRatings = %d\n", nRatings);
	printf("mu = %lg\n", mu);

	char vfile[256];
	strcpy(vfile, tmpdir);
	strcat(vfile,"values");
	FILE *tmp = fopen(vfile, "w");
	fprintf(tmp, "%u %u %u %u %u %25.18lg\n", nRatings, nUsers, nItems, nValidations, nTests, mu);
	for(int i = 0 ; i < nUsers; i++ ) {
		fprintf(tmp, "%d\n", users[i].id);
	}
	for(int i = 0 ; i < nItems; i++ ) {
		fprintf(tmp, "%d\n", items[i].id);
	}
	fclose(tmp);
}
Exemplo n.º 13
0
 Node* getChild(str s) {
   if (children.count(s)) return children[s];
   else return NULL;
 }
Exemplo n.º 14
0
ll dfs(int dep,int now,int *b)
{
    if (now==m+1)
    {
        int a[10];
        for (int i=1;i<=m;i++)
        {
            a[i]=(dep-1)*m+i;
            f[a[i]]=a[i];
            if (b[i]!=26)
                f[b[i]]=b[i];
        }
        for (int i=1;i<=m;i++)
            if (U[c[dep][i]])
                Union(a[i],b[i]);
        for (int i=2;i<=m;i++)
            if (L[c[dep][i]] && R[c[dep][i-1]])
                Union(a[i],a[i-1]);
        for (int i=m-1;i;i--)
            if (R[c[dep][i]] && L[c[dep][i+1]])
                Union(a[i],a[i+1]);
        for (int i=1;i<=m;i++)
            a[i]=find(a[i]);
        if (dep==n)
            return(a[m]==1);
        else
        {
            bool flag=false;
            for (int i=1;i<=m;i++)
            {
                if (!D[c[dep][i]])
                    a[i]=26;
                if (a[i]==1)
                    flag=true;
            }
            ull state=0;
            state=dep*lq+now;
            for (int i=1;i<=m;i++)
                state=state*lq+a[i];
            for (int i=1;i<=12;i++)
                state=state*lq+(v[i]+1);
            if (M.count(state))
                return(M[state]);
            ll ret=0;
            if (flag)
                ret=dfs(dep+1,1,a);
            M[state]=ret;
            return(ret);
        }
    }
    ll ret=0;
    for (int i=1;i<=12;i++)
    {
        if (v[i]==0)
            continue;
        v[i]--;
        c[dep][now]=i;
        ret+=dfs(dep,now+1,b);
        v[i]++;
    }
    return(ret);
}
Exemplo n.º 15
0
unsigned BUDataStructures::calculateGraphs(const Function *F,
                                           std::vector<const Function*> &Stack,
                                           unsigned &NextID,
                                           hash_map<const Function*, unsigned> &ValMap) {
  assert(!ValMap.count(F) && "Shouldn't revisit functions!");
  unsigned Min = NextID++, MyID = Min;
  ValMap[F] = Min;
  Stack.push_back(F);

  // FIXME!  This test should be generalized to be any function that we have
  // already processed, in the case when there isn't a main or there are
  // unreachable functions!
  if (F->isDeclaration()) {   // sprintf, fprintf, sscanf, etc...
    // No callees!
    Stack.pop_back();
    ValMap[F] = ~0;
    return Min;
  }

  DSGraph* Graph = getOrFetchDSGraph(F);

  // Find all callee functions.
  std::vector<const Function*> CalleeFunctions;
  GetAllAuxCallees(Graph, CalleeFunctions);
  std::sort(CalleeFunctions.begin(), CalleeFunctions.end());
  std::vector<const Function*>::iterator uid = std::unique(CalleeFunctions.begin(), CalleeFunctions.end());
  CalleeFunctions.resize(uid - CalleeFunctions.begin());

  // The edges out of the current node are the call site targets...
  for (unsigned i = 0, e = CalleeFunctions.size(); i != e; ++i) {
    const Function *Callee = CalleeFunctions[i];
    unsigned M;
    // Have we visited the destination function yet?
    hash_map<const Function*, unsigned>::iterator It = ValMap.find(Callee);
    if (It == ValMap.end())  // No, visit it now.
      M = calculateGraphs(Callee, Stack, NextID, ValMap);
    else                    // Yes, get it's number.
      M = It->second;
    if (M < Min) Min = M;
  }

  assert(ValMap[F] == MyID && "SCC construction assumption wrong!");
  if (Min != MyID)
    return Min;         // This is part of a larger SCC!

  // If this is a new SCC, process it now.
  if (Stack.back() == F) {           // Special case the single "SCC" case here.
    DEBUG(errs() << "Visiting single node SCC #: " << MyID << " fn: "
	  << F->getName() << "\n");
    Stack.pop_back();
    DEBUG(errs() << "  [BU] Calculating graph for: " << F->getName()<< "\n");
    calculateGraph(Graph);
    DEBUG(errs() << "  [BU] Done inlining: " << F->getName() << " ["
	  << Graph->getGraphSize() << "+" << Graph->getAuxFunctionCalls().size()
	  << "]\n");

    if (MaxSCC < 1) MaxSCC = 1;

    // Should we revisit the graph?  Only do it if there are now new resolvable
    // callees or new callees
    GetAllAuxCallees(Graph, CalleeFunctions);
    if (CalleeFunctions.size()) {
      DEBUG(errs() << "Recalculating " << F->getName() << " due to new knowledge\n");
      ValMap.erase(F);
      return calculateGraphs(F, Stack, NextID, ValMap);
    } else {
      ValMap[F] = ~0U;
      return MyID;
    }
  } else {
    // SCCFunctions - Keep track of the functions in the current SCC
    //
    std::vector<DSGraph*> SCCGraphs;

    unsigned SCCSize = 1;
    const Function *NF = Stack.back();
    ValMap[NF] = ~0U;
    DSGraph* SCCGraph = getDSGraph(NF);

    // First thing first, collapse all of the DSGraphs into a single graph for
    // the entire SCC.  Splice all of the graphs into one and discard all of the
    // old graphs.
    //
    while (NF != F) {
      Stack.pop_back();
      NF = Stack.back();
      ValMap[NF] = ~0U;

      DSGraph* NFG = getDSGraph(NF);

      if (NFG != SCCGraph) {
        // Update the Function -> DSG map.
        for (DSGraph::retnodes_iterator I = NFG->retnodes_begin(),
               E = NFG->retnodes_end(); I != E; ++I)
          setDSGraph(I->first, SCCGraph);
        
        SCCGraph->spliceFrom(NFG);
        delete NFG;
        ++SCCSize;
      }
    }
    Stack.pop_back();

    DEBUG(errs() << "Calculating graph for SCC #: " << MyID << " of size: "
	  << SCCSize << "\n");

    // Compute the Max SCC Size.
    if (MaxSCC < SCCSize)
      MaxSCC = SCCSize;

    // Clean up the graph before we start inlining a bunch again...
    SCCGraph->removeDeadNodes(DSGraph::KeepUnreachableGlobals);

    // Now that we have one big happy family, resolve all of the call sites in
    // the graph...
    calculateGraph(SCCGraph);
    DEBUG(errs() << "  [BU] Done inlining SCC  [" << SCCGraph->getGraphSize()
	  << "+" << SCCGraph->getAuxFunctionCalls().size() << "]\n"
	  << "DONE with SCC #: " << MyID << "\n");

    // We never have to revisit "SCC" processed functions...
    return MyID;
  }

  return MyID;  // == Min
}