int shortest_palindrome (int *s, int start, int end) { if (start > end) return INT_MAX; if (start == end) return 0; if (start == end-1 && s[start] == s[end]) return 0; if (start == end-1 && s[start] != s[end]) return 1; if (s[start] == s[end]) return shortest_palindrome (s, start+1, end-1); else return (1 + min ( shortest_palindrome (s, start+1, end), shortest_distance (s, start, end-1))); }
void TagViewer::mousePressEvent( QMouseEvent* e ) { if( e->button() != Qt::LeftButton ) { return; } if( tagging_ ) { tag_start_ = e->pos(); enforce_boundary_conditions( tag_start_ ); tag_end_ = tag_start_; } else if( untagging_ ) { // searches the closest rectangle to the picked point // removes it only if point is deemed close enough // there won't be tons of label per image, so a brute force search // is perfectly acceptable, no need to go into quad-tree float scale_f = scale_factor(); QRect rect_found; QString label_found; int distance_min = 200. / scale_f; QPoint p = e->pos(); enforce_boundary_conditions( p ); p /= scale_f; for( QList<TagDisplayElement>::iterator tag_itr = elts_.begin(); tag_itr != elts_.end(); ++tag_itr ) { const TagDisplayElement& tag = *tag_itr; const QList<QRect>& bbox = tag._bbox; for( QList<QRect>::const_iterator bbox_itr = bbox.begin(); bbox_itr != bbox.end(); ++bbox_itr ) { int d = shortest_distance( p, *bbox_itr ); if( d < distance_min ) { distance_min = d; label_found = tag._label; rect_found = *bbox_itr; } } } if( !label_found.isEmpty() && rect_found.isValid() ) { emit( untagged( label_found, rect_found ) ); } } }
// ./next_gen "# of permutations" stem0 stem1 stem2 ... int main(int argc, unsigned char **argv) { char phrase[59] = "I would much rather hear more about your whittling project"; unsigned int permutations = atou((char *)argv[1]); sseK00_19 = _mm_set1_epi32(0x5a827999); sseK20_39 = _mm_set1_epi32(0x6ed9eba1); sseK40_59 = _mm_set1_epi32(0x8f1bbcdc); sseK60_79 = _mm_set1_epi32(0xca62c1d6); best_stem = malloc(sizeof(char) * 5); shortest_d = 180; // Get finished context for phrase SHA_CTX *phrase_ctx = malloc(sizeof(SHA_CTX)); sha1_full(phrase_ctx, phrase); // Load prefixes unsigned char **prefixes = malloc(sizeof(char *) * PREFIX_COUNT); int p = 0; for(p = 0;p < PREFIX_COUNT;p++) prefixes[p] = argv[2 + p]; // Get chaining contexts for suffixes SHA_CTX *prefix_ctxs = malloc(sizeof(SHA_CTX) * PREFIX_COUNT); for(p = 0;p < PREFIX_COUNT;p++) sha1_partial(&prefix_ctxs[p], prefixes[p]); struct vector_ctx *vc = malloc(sizeof(struct vector_ctx) * PREFIX_COUNT/4); struct vector_ctx *vc_ptr = vc; SHA_CTX *prefix_ptr = prefix_ctxs; for(p = 0;p < PREFIX_COUNT;p+=4) { vectorize_prefixes(vc_ptr, prefix_ptr); prefix_ptr += 4; vc_ptr += 1; } // Allocate memory for expanded message template uint32_t *w = malloc(sizeof(uint32_t) * 80); int w_i = 0; // We only hash suffixes that are 5 bytes long // w[0] prefix_stem // w[1] current final char + some other stuff and zeros // w[2]-w[14] // Expanded message blocks 2-14 are always 0x0000000... for(w_i = 2;w_i < 15;w_i++) w[w_i] = 0; // w[15] is the size of the message w[15] = 552; // w[16] - stem constant - W13 ^ W8 ^ W2 ^ W0 => W0 <<< 1 // w[17] - changing lots // w[18] - constant w[18] = ROTATE(w[15], 1); // w[19] - stem constant // w[20] - changing lots uint32_t *stem_w = malloc(sizeof(uint32_t) * 80); uint32_t *stem_x = malloc(sizeof(uint32_t) * 80); init_lut(); struct vector_ctx *my_vc = malloc(sizeof(struct vector_ctx) * PREFIX_COUNT/2); int i = 0; char suffix_stem[5] = "!!!!"; for(i = 0;i < permutations;i++) { memcpy(stem_w, w, 80 * sizeof(uint32_t)); memcpy(my_vc, vc, sizeof(struct vector_ctx) * PREFIX_COUNT/2); int dist = shortest_distance(phrase_ctx, my_vc, suffix_stem, stem_w, stem_x); next_stem(suffix_stem); } free(vc); free(my_vc); free(w); free(stem_w); free(stem_x); // Print shortest_distance and the 5 char ending. printf("%d,%s,%d\n", shortest_d, best_stem, best_last+33); }
int main() { /*char c;*/ int choice; int n; char str[] = "gee ks f or g ee ks "; char path[128]; char S[128], T[128]; char *pattern = "-20"; char str1[32], str2[32]; char **s; /*do {*/ printf("MENU OPTIONS\n"); printf("1 -- remove spaces from string\n"); printf("2-- Check if a given sequence of moves for a robot is circular or not\n"); printf("3 -- Regex matching problem\n"); printf("4 -- Palindrome detection with non-alphanumeric characters\n"); printf("5 -- Normalize the path\n"); printf("6 -- replace space by percentage20 in a string\n"); printf("7 -- minimum window substring\n"); printf("8 -- integer to english words\n"); printf("9 -- restore IP addresses\n"); printf("10 -- check if strings are isomorphic\n"); printf("11 -- function to determine if a string is a valid number without using any built-in function\n"); printf("12 -- reverse string\n"); printf("13 -- reverse words in a sentence\n"); printf("14 -- shortest distance between words\n"); printf("15 -- shortest distance between words\n"); printf("\n"); printf("Enter your choice\n"); scanf("%d",&choice); switch(choice){ case 1: removeSpaces(str); printf("%s", str); break; case 2: printf("Enter path\n"); scanf("%s", path); printf("path is circular: %s", checkCircularpath(path)?"yes":"no"); break; case 4: palindrome(); break; case 5: printf("Enter path\n"); fgets(path, 128, stdin); printf("Normalized path: %s\n", normalize(path)); break; case 6: memset(path, '\0', 128); printf("Enter string\n"); scanf("%s", path); /*gets(path);*/ replace_spaces(path, pattern); printf("%s\n", path); break; case 7: printf("Enter the string\n"); scanf("%s", S); printf("Enter the pattern\n"); scanf("%s", T); min_window_substring(S, T); break; case 8: /*interger_to_english_words();*/ break; case 9: restore_ip_address(); break; case 10: printf("Enter strings of equal length\n"); printf("Enter string 1\n"); scanf("%s", S); printf("Enter string 2\n"); scanf("%s", T); printf("Strings are isomorphic : %s\n", isomorphic_strings(S, T)?"Yes":"No"); break; case 11: printf("Enter the string\n"); scanf(" %[^\n]s", S); //reading a space through scanf /*fgets(stdin, S, sizeof(S));*/ printf("Is number : %s\n", is_valid_number(S)?"yes":"no"); break; case 12: printf("Enter the string\n"); scanf(" %[^\n]s", S); //make scanf work with spaces //make scanf work with spaces reverse_string(S, strlen(S)); print_string(S, strlen(S)); break; case 13: printf("Enter the sentence\n"); scanf(" %[^\n]s", S); //make scanf work with spaces //make scanf work with spaces /*fgets(S, 128, stdin);*/ reverse_words(S); print_string(S, strlen(S)); break; case 14: printf("Enter number of words\n"); scanf("%d", &n); s = create_2Dchar_array(n, 128); input_2Dchar_array(s, n, 128); printf("enter word 1\n"); scanf("%s", str1); printf("enter word 2\n"); scanf("%s", str2); printf("Shortest distance between %s and %s : %d\n", str1, str2, shortest_distance(s, n, str1, str2)); break; default: printf("Invalid option\n"); break; } printf("\n\n"); /*}while((c=getchar())!='q'); */ return 0; }
float mglLine::distance_to( int x, int y ) { return shortest_distance( (float)x, (float)y, (float)x1, (float)y1, (float)x2, (float)y2 ); }