1. 求两个字符串的最大公共字符串
#include <iostream>
using namespace std;
void getLength(const char *src1, const char *src2, int *l1, int *l2)
{
if(src1 == NULL || src2 == NULL || strlen(src1) == 0 || strlen(src2) == 0)
{
*l1 = 0;
*l2 = 0;
return ;
}
int pl1 = 0,pl2 = 0;
int tpl1 = 0,tpl2 = 0;
int tpl3 = 0,tpl4 = 0;
if(src1[0] == src2[0])
{
getLength(src1+1,src2+1, &tpl1, &tpl2);
pl1 = tpl1+1;
}
else
{
pl1 = 0;
getLength(src1+1,src2, &tpl1, &tpl2);
getLength(src1,src2+1, &tpl3, &tpl4);
}
*l1 = pl1;
*l2 = tpl2>tpl4?tpl2:tpl4;
if(*l2<pl1)
*l2 = pl1;
}
int main(int argc, char **argv)
{
int l1 =0 , l2 =0 ;
getLength("abcdefg","cdefbcderg", &l1, &l2);
cout<<l2<<endl;
}
using namespace std;
void getLength(const char *src1, const char *src2, int *l1, int *l2)
{
if(src1 == NULL || src2 == NULL || strlen(src1) == 0 || strlen(src2) == 0)
{
*l1 = 0;
*l2 = 0;
return ;
}
int pl1 = 0,pl2 = 0;
int tpl1 = 0,tpl2 = 0;
int tpl3 = 0,tpl4 = 0;
if(src1[0] == src2[0])
{
getLength(src1+1,src2+1, &tpl1, &tpl2);
pl1 = tpl1+1;
}
else
{
pl1 = 0;
getLength(src1+1,src2, &tpl1, &tpl2);
getLength(src1,src2+1, &tpl3, &tpl4);
}
*l1 = pl1;
*l2 = tpl2>tpl4?tpl2:tpl4;
if(*l2<pl1)
*l2 = pl1;
}
int main(int argc, char **argv)
{
int l1 =0 , l2 =0 ;
getLength("abcdefg","cdefbcderg", &l1, &l2);
cout<<l2<<endl;
}
2. Convert a binary search tree to a circular sorted linked list. The highest valued node should point to the lowest valued node at each step. 下面只是把BST转为单向链表,而且没有处理left指针,可以再完善一下。
#include <iostream>
using namespace std;
struct BSTNode
{
BSTNode(int v){value = v;left = right = NULL;}
int value;
BSTNode *left;
BSTNode *right;
};
void ConvertBST2CiruluarLinkedList(BSTNode *root, BSTNode **head, BSTNode **tail)
{
if(root == NULL)
return;
BSTNode *headl = NULL, *taill = NULL;
if(root->left != NULL)
{
ConvertBST2CiruluarLinkedList(root->left,&headl, &taill);
}
if(headl != NULL)
{
*head = headl;
taill->right = root;
}
else
{
*head = root;
}
BSTNode *headr = NULL, *tailr = NULL;
if(root->right !=NULL)
{
ConvertBST2CiruluarLinkedList(root->right,&headr, &tailr);
}
if(headr != NULL)
{
root->right = headr; //anyway, we should use root to make connection with right linked list;
tailr->right = *head;
*tail = tailr;
}
else
{
*tail = root;
(*tail)->right = *head;
}
}
int main(int argc, char **argv)
{
BSTNode *a4 = new BSTNode (4);
BSTNode *a3 = new BSTNode (3);
BSTNode *a2 = new BSTNode (2);
BSTNode *a1 = new BSTNode (1);
BSTNode *a5 = new BSTNode (5);
a2->left = a1;
a2->right = a3;
a4->left = a2;
a4->right = a5;
BSTNode * head = NULL, *tail = NULL;
ConvertBST2CiruluarLinkedList(a4,&head,&tail);
}
using namespace std;
struct BSTNode
{
BSTNode(int v){value = v;left = right = NULL;}
int value;
BSTNode *left;
BSTNode *right;
};
void ConvertBST2CiruluarLinkedList(BSTNode *root, BSTNode **head, BSTNode **tail)
{
if(root == NULL)
return;
BSTNode *headl = NULL, *taill = NULL;
if(root->left != NULL)
{
ConvertBST2CiruluarLinkedList(root->left,&headl, &taill);
}
if(headl != NULL)
{
*head = headl;
taill->right = root;
}
else
{
*head = root;
}
BSTNode *headr = NULL, *tailr = NULL;
if(root->right !=NULL)
{
ConvertBST2CiruluarLinkedList(root->right,&headr, &tailr);
}
if(headr != NULL)
{
root->right = headr; //anyway, we should use root to make connection with right linked list;
tailr->right = *head;
*tail = tailr;
}
else
{
*tail = root;
(*tail)->right = *head;
}
}
int main(int argc, char **argv)
{
BSTNode *a4 = new BSTNode (4);
BSTNode *a3 = new BSTNode (3);
BSTNode *a2 = new BSTNode (2);
BSTNode *a1 = new BSTNode (1);
BSTNode *a5 = new BSTNode (5);
a2->left = a1;
a2->right = a3;
a4->left = a2;
a4->right = a5;
BSTNode * head = NULL, *tail = NULL;
ConvertBST2CiruluarLinkedList(a4,&head,&tail);
}