竞赛题: 回文问题

来源:互联网 发布:图像识别算法matlab 编辑:程序博客网 时间:2024/06/05 10:03

哎。今天知道了个网站,好不容易刷完了一题想上传了却不知道那题题目叫什么。。。还是贴我博客上让我心里平衡点吧

题目如下:

我写的代码:

#include <stdio.h>#include <string.h>#pragma warning( disable : 4996 ) inline char * strFitted( char ** ppStr ) {char * strSrc = * ppStr;* ppStr = new char[ strlen( * ppStr ) + 1 ];strcpy( * ppStr, strSrc );delete [] strSrc;return * ppStr;}inline void strZeroMemory( char ** ppStr ) {memset( * ppStr, 0, sizeof( * ppStr ) );}char * strReverse( char ** ppStr ) {for ( char * pEnd = &( * ppStr )[ strlen( * ppStr ) - 1 ] , * pHead = ( * ppStr );pEnd > pHead; --pEnd, ++pHead ) {* pEnd ^= * pHead;* pHead ^= * pEnd;* pEnd ^= * pHead;}return * ppStr;}inline bool IsPalindrome( char * pStr ) {char * pSrc = new char[ 1000 ];strcpy( pSrc, pStr );bool IsPali = strcmp( strReverse( & pStr ), pSrc );delete [] pSrc;return ! IsPali;}void Proc( char ** ppStrSum, char * pStr ) {char * pWord = new char[ 1000 ];int nWordIndex = 0;strZeroMemory( & pWord );for ( size_t i = 0; i < strlen( pStr ) + 1; ++i ) {if ( * ( pStr + i ) == ' ' || * ( pStr + i ) == '\0' ) { // space fetchedif ( IsPalindrome( pWord ) ) {strcat( * ppStrSum, pWord );strcat( * ppStrSum, " " );}strZeroMemory( & pWord );nWordIndex = 0;} else {* ( pWord + nWordIndex ) = * ( pStr + i );* ( pWord + nWordIndex + 1 ) = '\0';++nWordIndex;}}delete [] pWord;}int main() {int nNum = 0;char * pStrSum = new char[ 100000 ];strZeroMemory( & pStrSum );//printf_s( "Enter the number of string:" );scanf( "%i", & nNum );scanf( "%*c" );//printf_s( "--------------------\n" );for ( size_t i = 0; i < nNum; ++i ) {char * pStr = new char[ 1000 ];//printf_s( "No%d:\n", i );//printf_s( "Enter the string:\n" );strZeroMemory( & pStr );gets( pStr );strFitted( & pStr );Proc( & pStrSum, pStr );//printf_s( "***Current Blocks: %s ***\n\n", pStrSum );delete [] pStr;}printf_s( "%s", pStrSum );delete [] pStrSum;return 0;}
运行没有什么问题

0 0
原创粉丝点击