Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- char * getCharId(int id)
- {
- char *charId = (char *)malloc(len[i]);
- int t = ids[i], j = 0, l = len[i];
- while (--l >= 0)
- {
- charIds[i][l] = t % 10;
- t /= 10;
- }
- }
- void locateIds(int *ids, int size)
- {
- int *count = (int*)calloc(size * sizeof(int));
- char **charIds = (char **)malloc(size * sizeof(char *));
- char **pos = (char**)calloc(size * sizeof(char *));
- char c;
- int i;
- // precalculate character ids
- for (i = 0; i < size; i++)
- {
- pos[i] = charIds[i] = getCharId(ids[i]);
- }
- while ((c = nextChar()) != EOF)
- {
- for (int i = 0; i < size; i++)
- {
- char *id = charIds[i];
- if (*pos[i] == c)
- {
- if (*(++pos[i]) == '\0')
- {
- pos[i] = id;
- count[i]++;
- }
- }
- else
- {
- pos[i] = id;
- }
- }
- }
- // print matches
- for (i = 0; i < size; i++)
- {
- printf("%d: %d\n", ids[i], count[i]);
- }
- // cleanup
- free(pos);
- free(count);
- for (i = 0; i < size; i++)
- {
- free(charIds[i]);
- }
- free(charIds);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement