1. 原地reverse一个字符串
2. 求strA和strB最长公共子串
求解:
引进一个二维数组c[][],用c[i][j]记录X[i]与Y[j] 的LCS 的长度,b[i][j]记录c[i][j]是通过哪一个子问题的值求得的,以决定输出最长公共字串时搜索的方向。
我们是自底向上进行递推计算,那么在计算c[i,j]之前,c[i-1][j-1],c[i-1][j]与c[i][j-1]均已计算出来。此时我们根据X[i] == Y[j]还是X[i] != Y[j],就可以计算出c[i][j]。
问题的递归式写成:
回溯输出最长公共子序列过程:
算法分析:
由于每次调用至少向上或向左(或向上向左同时)移动一步,故最多调用(m + n)次就会遇到i = 0或j = 0的情况,此时开始返回。返回时与递归调用时方向相反,步数相同,故算法时间复杂度为Θ(m + n)。
完整的实现代码如下:
/**
找出两个字符串的最长公共子串的长度
** author :liuzhiwei
** data :2011-08-15
**/
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
int LCSLength(char* str1, char* str2, int **b)
{
int i,j,length1,length2,len;
length1 = strlen(str1);
length2 = strlen(str2);
//双指针的方法申请动态二维数组
int **c = new int*[length1+1]; //共有length1+1行
for(i = 0; i < length1+1; i++)
c[i] = new int[length2+1]; //共有length2+1列
for(i = 0; i < length1+1; i++)
c[i][0]=0; //第0列都初始化为0
for(j = 0; j < length2+1; j++)
c[0][j]=0; //第0行都初始化为0
for(i = 1; i < length1+1; i++)
{
for(j = 1; j < length2+1; j++)
{
if(str1[i-1]==str2[j-1]) //由于c[][]的0行0列没有使用,c[][]的第i行元素对应str1的第i-1个元素
{
c[i][j]=c[i-1][j-1]+1;
b[i][j]=0; //输出公共子串时的搜索方向
}
else if(c[i-1][j]>c[i][j-1])
{
c[i][j]=c[i-1][j];
b[i][j]=1;
}
else
{
c[i][j]=c[i][j-1];
b[i][j]=-1;
}
}
}
/*
for(i= 0; i < length1+1; i++)
{
for(j = 0; j < length2+1; j++)
printf("%d ",c[i][j]);
printf("\n");
}
*/
len=c[length1][length2];
for(i = 0; i < length1+1; i++) //释放动态申请的二维数组
delete[] c[i];
delete[] c;
return len;
}
void PrintLCS(int **b, char *str1, int i, int j)
{
if(i==0 || j==0)
return ;
if(b[i][j]==0)
{
PrintLCS(b, str1, i-1, j-1); //从后面开始递归,所以要先递归到子串的前面,然后从前往后开始输出子串
printf("%c",str1[i-1]); //c[][]的第i行元素对应str1的第i-1个元素
}
else if(b[i][j]==1)
PrintLCS(b, str1, i-1, j);
else
PrintLCS(b, str1, i, j-1);
}
int main(void)
{
char str1[100],str2[100];
int i,length1,length2,len;
printf("请输入第一个字符串:");
gets(str1);
printf("请输入第二个字符串:");
gets(str2);
length1 = strlen(str1);
length2 = strlen(str2);
//双指针的方法申请动态二维数组
int **b = new int*[length1+1];
for(i= 0; i < length1+1; i++)
b[i] = new int[length2+1];
len=LCSLength(str1,str2,b);
printf("最长公共子串的长度为:%d\n",len);
printf("最长公共子串为:");
PrintLCS(b,str1,length1,length2);
printf("\n");
for(i = 0; i < length1+1; i++) //释放动态申请的二维数组
delete[] b[i];
delete[] b;
system("pause");
return 0;
}
3. 合并两个有序的链表。
方法一使用递归的方法
struct Node
{
Node* next;
int value;
};
Node* Merge(Node* head1, Node* head2)
{
Node* res;
if(head1 == NULL) return head2;
if(head2 == NULL) return head1;
if(head1->value < head2->value)
{
res = head1;
res->next = Merge(head1->next, head2);
}
else
{
res = head2;
res->next = Merge(head1, head2->next);
}
return res;
}
4. 删除双链表当中的一个指点节点。
5. 原定转置一个链表。
typedef struct node
{
struct node* next;
datatype data;
}Node;
Node* reverse(Node *head)
{
if(head == NULL || head->next == NULL)
{
return head;
}
Node *p1, *p2, *p3;
p1 = head;
p2 = head->next;
head->next = NULL;
while(p2 != NULL)
{
p3 = p2->next;
p2->next = p1;
p1 = p2;
p2 = p3;
}
head = p1;
return head;
}
6. 找到一棵BST树中第三大的数,但不能将这棵BST数据存到一个额外的结构(例如数组)再进行查找。
给一个二叉查找树(BST),找出第 k 大的值。比如:
该图中,第3大的值是10.
分析:
我们可以通过类似后序遍历的方法把BST从大到小排序,然后,就可以得到第 k 大的值了。代码如下:
//get the nth largest value in BST public void getNthnode(Node root, int n) { if (root == null) return; getNthnode(root.rightChild, n); k++; if (k == n) { System.out.print(root.toString()); } getNthnode(root.leftChild, n); }