字符串原始匹配和二叉树相等算法
2009-09-14 19:05
316 查看
1.原始字符串匹配,修改
//修改后二叉树
//--------------------------------------------xfz
//-----------------------------xfz
#include "stdafx.h" #include "iostream" using namespace std; int Index(char S[],int Slen,char T[],int Tlen,int pos); int main(int argc, char* argv[]) { char ss[]="helloworld"; char tt[]="wor"; int slen=sizeof(ss); int tlen=sizeof(tt); int a=0; a=Index(ss,slen,tt,tlen,0); cout<<a<<endl;//将1修改为0 return 0; } int Index(char S[],int Slen,char T[],int Tlen,int pos) { int i=pos; int j=0; while(i<=Slen && j<=Tlen&&T[j]!='/0') { if(S[i]==T[j]) { //++i; //++j; i++; j++; } else { //i=i-j+2;//这句是什么意思啊? i++; //j=1; j=0; } } // if(j>Tlen) // return i-Tlen; // else // return 0; // cout<<i-j<<endl; return i-j; }
//修改后二叉树
// Test.cpp : Defines the entry point for the console application. // #include "stdafx.h" // int main(int argc, char* argv[]) // }// { // printf("Hello World!/n"); // return 0; #include <stdio.h> #include <malloc.h> #include <iostream.h> typedef struct node //二叉树的链式存储 { char c; struct node *leftchild, *rightchild; } BinTNode,*BinTree; int CompTree(BinTree tree1, BinTree tree2) { //判断两棵二叉树是否相等,用递归算法 BinTree RootA=tree1; BinTree RootB=tree2; // while(RootA && RootB) if (RootA&&RootB) { if(RootA->c==RootB->c) { // RootA=RootA->leftchild; // RootB=RootB->leftchild; CompTree(RootA->leftchild, RootB->leftchild); //继续判断右子树 // RootA=RootA->rightchild; // RootB=RootB->rightchild; CompTree(RootA->rightchild, RootB->rightchild); } else return -1; } if (RootA!=NULL || RootB!=NULL) return -1; return 0; } void main(void) { //手动建立root1,三个节点,a b c BinTree root1 =(BinTNode*)malloc(sizeof(BinTNode)); BinTNode *lch1 =(BinTNode*)malloc(sizeof(BinTNode)); BinTNode *rch1 =(BinTNode*)malloc(sizeof(BinTNode)); root1->c='a'; root1->leftchild=lch1; root1->rightchild=rch1; lch1->c='b'; lch1->leftchild=NULL; lch1->rightchild=NULL; rch1->c='c'; rch1->leftchild=NULL; rch1->rightchild=NULL; //手动建立root2,三个节点,a b c BinTree root2 =(BinTNode*)malloc(sizeof(BinTNode)); BinTNode *lch2 =(BinTNode*)malloc(sizeof(BinTNode)); BinTNode *rch2 =(BinTNode*)malloc(sizeof(BinTNode)); root2->c='a'; root2->leftchild=lch2; root2->rightchild=rch2; lch2->c='b'; lch2->leftchild=NULL; lch2->rightchild=NULL; rch2->c='c'; rch2->leftchild=NULL; rch2->rightchild=NULL; cout<<CompTree(root1, root2)<<endl; }
//--------------------------------------------xfz
int Index(char S[],int Slen,char T[],int Tlen,int pos) { int i=pos; int j=0; while(i<=Slen && j<=Tlen) { if(S[i]==T[j]) { ++i; ++j; } else { i=i-j+2; j=1; } } if(j>Tlen) return i-Tlen; else return 0; } void main() { char ss[]="helloworld"; char tt[]="wor"; int slen=sizeof(ss); int tlen=sizeof(tt); cout<<Index(ss,slen,tt,tlen,1); }
//-----------------------------xfz
typedef struct node { //二叉树的链式存储 char c; struct node *leftchild, *rightchild; } BinTNode,*BinTree; int CompTree(BinTree tree1, BinTree tree2) {//判断两棵二叉树是否相等,用递归算法 BinTree RootA=tree1; BinTree RootB=tree2; while(RootA && RootB) { if(RootA->c==RootB->c) { RootA=RootA->leftchild; RootB=RootB->leftchild; CompTree(RootA, RootB); RootA=RootA->rightchild; RootB=RootB->rightchild; CompTree(RootA, RootB); } else return -1; } if (RootA!=NULL || RootB!=NULL) return -1; return 0; } void main(void) { 手动建立root1,三个节点,a b c BinTree root1 =(BinTNode*)malloc(sizeof(BinTNode)); BinTNode *lch1 =(BinTNode*)malloc(sizeof(BinTNode)); BinTNode *rch1 =(BinTNode*)malloc(sizeof(BinTNode)); root1->c='a'; root1->leftchild=lch1; root1->rightchild=rch1; lch1->c='b'; lch1->leftchild=NULL; lch1->rightchild=NULL; rch1->c='c'; rch1->leftchild=NULL; rch1->rightchild=NULL; 手动建立root2,三个节点,a b c BinTree root2 =(BinTNode*)malloc(sizeof(BinTNode)); BinTNode *lch2 =(BinTNode*)malloc(sizeof(BinTNode)); BinTNode *rch2 =(BinTNode*)malloc(sizeof(BinTNode)); root2->c='a'; root2->leftchild=lch2; root2->rightchild=rch2; lch2->c='b'; lch2->leftchild=NULL; lch2->rightchild=NULL; rch2->c='c'; rch2->leftchild=NULL; rch2->rightchild=NULL; cout<<CompTree(root1, root2); }
相关文章推荐
- 字符串--KMP模式匹配算法
- 字符串模式匹配算法1 - BF和KMP算法
- 彻头彻尾地理解KMP字符串模式匹配算法
- 用一道例题来分享两种匹配字符串的算法!
- 字符串模式匹配算法——BM、Horspool、Sunday、KMP、KR、AC算法一网打尽
- 用一道例题来分享两种匹配字符串的算法!
- 字符串模式匹配算法2 - AC算法
- [代码实例]字符串近似匹配算法 C++
- Google字符串模糊匹配算法,字典树模糊查询
- 字符串多模式精确匹配(脏字/敏感词汇/关键字过滤算法)——TTMP算法 之实战F模式
- 2017.1.10 算法测试题集 - 1002 - 相等二叉树
- 字符串模式匹配算法——BM、Horspool、Sunday、KMP、KR、AC算法
- KMP 字符串的匹配算法
- KMP算法-字符串模式匹配算法
- 【算法学习】horspool查找匹配字符串
- KMP 字符串模式匹配算法
- 实现一个挺高级的字符匹配算法: 给一串很长字符串,要求找到符合要求的字符串,例如目的串:123 1******3***2 ,12*****3这些都要找出来
- 简单字符串模式匹配算法的C++实现
- 一种比KMP更优的字符串模式匹配算法
- 【算法学习】KMP查找匹配字符串