
26-12-2008, 00:56
|
|
מנהל
|
|
חבר מתאריך: 26.07.08
הודעות: 6,473
|
|
|
עדכון חשוב
שכחתי להוסיף משהו חשוב מאוד לפונקציה..
אחרי שמצאת chunk אחד ועוברים לחיפוש chunk חדש, חשוב לאתחל את המשתנה chr.
אני לא יודע אם יש פונקציה מובנית עבור איתחול משתנים, לכן אוסיף הערה במקום המתאים שצריך לאתחל את המשתנה.
דבר נוסף, אחרי כל איטרציה יש להגדיר כ-true את ה-member המתאים במערך chr.
דבר אחרון, שיניתי את הסוג של המשתנה chr מ-char ל-short כי הערכים במערך מסמלים רק true או false...
הפונקציה הסופית (אני מקוה):
קוד:
void substr_unique(char *str)
{
/* PAY ATTENTION: i changed the chr type from `char` to `short`, because the values can be 0 OR 1 */
short chr[256]; // All the chars available in 8bit encoding are 256 chars (2^8).
int offset[100], length[100], // Let's hope that the string isn't too long...
idx=0; // Index in the subject string. first char has idx=0
short i=0,
topKey=0; // Has the key of the offset/length array. Indicates the longest string chunk.
while(*(str++) != '\n')
{
if (chr[(int)(*str)]) // This char already exist
{
offset[i] = idx;
if (i) // Is i>0?
{
length[i] = idx - offset[i-1] - length[i-1];
if (length[i] > length[i-1])
topKey = i;
}
else
{
length[i] = idx+1;
topKey = i;
}
++i;
/* CLEAN chr HERE (set all array members to be "Boolean false") */
}
else
chr[(int)(*str)] = 1; // "Boolean true"
++idx;
}
}
|