回复:throw brick to attract jade

/*
Author: tank

Disclaimer:
return value:
1 aaabbb
2 abbb
3 ab

Each key is unique to each other.
*/
#include
#include

#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif

#define KEY_1 "aaabbb"
#define KEY_2 "abbb"
#define KEY_3 "ab"

int ProcessKey(char * in_string, char * in_Key);

int ProcessString(char * in_String)
{
if(strstr(in_String, KEY_1))
{
if(ProcessKey(in_String, KEY_1) == TRUE)
return 1;
else
return -1;
}
else
if(strstr(in_String, KEY_2))
{
if(ProcessKey(in_String, KEY_2) == TRUE)
return 2;
else
return -1;
}
else
if(strstr(in_String, KEY_3))
{
if(ProcessKey(in_String, KEY_3) == TRUE)
return 3;
else
return -1;
}
return -1;
}

int ProcessKey(char * in_String, char * in_Key)
{
int n_BuffLength = strlen(in_Key) + 1;
int n_BuffCount = 0;
char * str_Buff = new char[n_BuffLength];
int n_ReturnStatus = FALSE;

if( strlen(in_String) % strlen(in_Key)) // different array length
return -1;
else
if(!strcmp(in_String, in_Key))
return TRUE;

memset(str_Buff, 0, n_BuffLength);

for(unsigned int n_Count = 0; n_Count {
if(n_BuffCount == strlen(in_Key)) // reset
{
if(strcmp(str_Buff, in_Key)) // different
{
n_ReturnStatus = FALSE;
break;
}
else
n_ReturnStatus = TRUE;

memset(str_Buff, 0, n_BuffLength);
n_BuffCount = 0;
}
str_Buff[n_BuffCount++] = in_String[n_Count];
}

delete str_Buff;

return n_ReturnStatus;
}

int main()
{
printf("%d\n", ProcessString(KEY_1));
printf("%d\n", ProcessString(KEY_2));
printf("%d\n", ProcessString(KEY_3));
printf("%d\n", ProcessString("aaabbbaaabbbaaabbbaaabbbaaabbbaaabbbaaabbbaaabbb"));
printf("%d\n", ProcessString("abbbabbbabbbabbbabbbabbbabbb"));
printf("%d\n", ProcessString("abababababababab"));
printf("%d\n", ProcessString("aaabbbaaabbbaaabbbaaabbbaaabbbaaabbbaaabbbaaabbbabbbabbbabbbabbbabbbabbbabbb"));
return TRUE;
}

///

Compiled and tried.

所有跟帖: 

回复:回复:you don't need to loop string three times! -sub101- 给 sub101 发送悄悄话 (0 bytes) () 02/13/2008 postreply 13:30:15

i never loop the string 3 times. -tank- 给 tank 发送悄悄话 (64 bytes) () 02/13/2008 postreply 13:31:33

回复:You are right. However, I have one more question, -sub101- 给 sub101 发送悄悄话 (19 bytes) () 02/13/2008 postreply 13:58:42

回复:回复:You are right. However, I have one more question, -tank- 给 tank 发送悄悄话 (207 bytes) () 02/13/2008 postreply 14:10:10

回复:回复:回复:I suck at algorithms. I discussed them -sub101- 给 sub101 发送悄悄话 (34 bytes) () 02/13/2008 postreply 14:13:59

the fastest solution should be using DFA -Jamesxu- 给 Jamesxu 发送悄悄话 (36 bytes) () 02/13/2008 postreply 17:30:31

回复:thanks! -sub101- 给 sub101 发送悄悄话 (0 bytes) () 02/13/2008 postreply 20:23:01

请您先登陆,再发跟帖!