Author |
Topic: Repeated String (Read 815 times) |
|
johny_cage
Full Member
Gender:
Posts: 155
|
|
Repeated String
« on: Nov 24th, 2007, 5:58am » |
Quote Modify
|
Find the first 4 character substring in a given string that occurs more than once. and return its starting index. E.g. for “abcdebcxbcdebyz” output should be “bcde” and index is 1.
|
|
IP Logged |
|
|
|
towr
wu::riddles Moderator Uberpuzzler
Some people are average, some are just mean.
Gender:
Posts: 13730
|
|
Re: Repeated String
« Reply #1 on: Nov 24th, 2007, 6:12am » |
Quote Modify
|
Seems like a duplicate of the thread Finding "Substring Position". Even the example used is the same, although Robin posed the question more generally.
|
« Last Edit: Nov 24th, 2007, 6:12am by towr » |
IP Logged |
Wikipedia, Google, Mathworld, Integer sequence DB
|
|
|
downloadprj
Newbie
Posts: 4
|
|
Re: Repeated String
« Reply #2 on: Nov 24th, 2007, 7:11am » |
Quote Modify
|
Following code finds the repeating substring of size 4 or less in the input string. int Substr(char str[], char sub[], int subSize) { for (int i = 0; str[i] != '\0'; i++) { for (int j = 0, k = i; (str[k] == sub[j]) && (j < subSize); j++, k++); if (j == subSize) return 1; } return 0; } int LongestRepeatingSubString(char str[]) { int length = 4; for (int sublen = length/2; sublen != 1; sublen--) { for (int i = 0; i + sublen <= length - sublen; i++) { if (Substr(str + sublen + i, str + i, sublen) == 1) { char t = str[i + sublen]; str[i + sublen] = '\0'; printf("%s\n", str + i); str[i + sublen] = t; return sublen; } } } }
|
« Last Edit: Nov 24th, 2007, 7:14am by downloadprj » |
IP Logged |
|
|
|
|