连连看消重算法
#include <iostream>
using namespace std;
// 连连看消重算法
void trytolink( char* str )
{
char* pcur = str;
// char* pos[20]= { 0 };
char** pos = (char**) malloc ( sizeof( char* ) * (strlen( str ) + 1) );
memset( pos, '\0', strlen( str ) + 1);
//char* pnodes = (char*)nodes;
bool state = 0;
size_t index = 0;
// int j = 0;
if ( !(*pcur) )
{
return;
}
// 只记位置的算法
// for( index = 0; index < strlen( str ); )
while( *pcur )
{
// 内容都退光了
if( index == 0 )
{
pos[0] = pcur++;
}
if ( *pos[index] == *pcur )
{
state = 1;
pcur++;
continue;
}
else
{
if ( state == 1 )
{
index--;
state = 0;
continue;
}
else
{
index++;
pos[index] = pcur++;
}
}
}
for( size_t j = 0 ;j < index;j++ )
{
str[j] = *pos[j];
};
delete pos;
}
void main()
{
char abc[30] = { "you should sleep at noon."};
trytolink( abc );
printf( "str:%s", abc );
system( "pause" );
}
using namespace std;
// 连连看消重算法
void trytolink( char* str )
{
char* pcur = str;
// char* pos[20]= { 0 };
char** pos = (char**) malloc ( sizeof( char* ) * (strlen( str ) + 1) );
memset( pos, '\0', strlen( str ) + 1);
//char* pnodes = (char*)nodes;
bool state = 0;
size_t index = 0;
// int j = 0;
if ( !(*pcur) )
{
return;
}
// 只记位置的算法
// for( index = 0; index < strlen( str ); )
while( *pcur )
{
// 内容都退光了
if( index == 0 )
{
pos[0] = pcur++;
}
if ( *pos[index] == *pcur )
{
state = 1;
pcur++;
continue;
}
else
{
if ( state == 1 )
{
index--;
state = 0;
continue;
}
else
{
index++;
pos[index] = pcur++;
}
}
}
for( size_t j = 0 ;j < index;j++ )
{
str[j] = *pos[j];
};
delete pos;
}
void main()
{
char abc[30] = { "you should sleep at noon."};
trytolink( abc );
printf( "str:%s", abc );
system( "pause" );
}
范晨鹏
------------------
软件是一种态度
成功是一种习惯