qey

Atitude is Everything.-- 关注C/C++,关注Linux(Unix) ,关注网络。 Better Late Than Never.
随笔 - 4, 文章 - 13, 评论 - 7, 引用 - 0
数据加载中……

寻找最长同字符子串

// sameCharForMore.cpp : Defines the entry point for the console application.
// 编写一个 C 函数,该函数在一个字符串中找到可能最长子字符串,
// 该字符串是由同一字符组成的

#include 
"stdafx.h"

#include 
"iostream"
#include 
"cassert"
using namespace std;

typedef 
struct chNode {
    
char* chPointer;
    
int count;
} CHNODE;

CHNODE FindMoreSameChars(
char* src)
{
    assert(
*src);
    
int curCount=1;
    CHNODE nodeHaveFound;
    
if (*src) {
        nodeHaveFound.chPointer 
=src;
        nodeHaveFound.count 
=1;
    }
    
while (*src++) {
        
if (*src ==*(src-1)) {
            curCount
++;
        }
        
else {
            
if (curCount >nodeHaveFound.count) {
                nodeHaveFound.chPointer 
=src-curCount;
                nodeHaveFound.count 
=curCount;
            }
            curCount 
=1;
        }
    }
    
return nodeHaveFound;
}

int main(int argc, char* argv[])
{
    CHNODE strNode;
    
char ch;
    
char* strForFind ="adfdddddllllfffffffflsjdfierrrrrrrrrrrrrrrlsdfls";

    strNode 
=FindMoreSameChars(strForFind);
    ch 
=*strNode.chPointer;
    cout
<<ch<<"\t"<<hex<<(int)strNode.chPointer<<endl
        
<<dec<<strNode.count<<endl;

    
//FindMoreSameChars("");    
    system("pause");
    
return 0;
}
/*  对于在栈中的字符串指针charPointer 指向堆区(new),要输出指针当前的字符,*charPointer
 *    输出指针地址用 &charPointer,要输出指针指向的地址用 (int)charPointer
 
*/

posted on 2008-11-13 19:55 无声无色 阅读(425) 评论(0)  编辑 收藏 引用 所属分类: 面试集合


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理