Skip to content

Instantly share code, notes, and snippets.

@hadronzoo
Last active January 4, 2016 07:39
Show Gist options
  • Save hadronzoo/8590356 to your computer and use it in GitHub Desktop.
Save hadronzoo/8590356 to your computer and use it in GitHub Desktop.
#include<stdio.h>
#include<stdlib.h>
int *add_counts(char *corpus, int *counts) {
int i = 0;
while (corpus[i]) {
counts[corpus[i]]++;
i++;
}
return counts;
}
int check_message(char *corpus, char *message) {
int corpus_freq[256];
int message_freq[256];
add_counts(corpus, corpus_freq);
int i = 0;
while (message[i]) {
char c = message[i];
int f = ++message_freq[c];
if (f > corpus_freq[c]) {
return -1;
}
i++;
}
return 0;
}
int main() {
static char note[] = "Give me money, or else...";
static char magazine[] = "The Weinstein Company pulled their Grace Kelly biopic Grace of Monaco from the release calendar Thursday. It’s the third release change in the past six months. Directed by Olivier Dahan and starring Nicole Kidman as the late princess, the film was originally set for a Nov. 27, 2013, release.";
if (check_message(magazine, note) < 0) {
printf("Source cannot be used to write note\n");
return -1;
}
else {
printf("Source can be used to write note\n");
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment