面试题之两个字符串

两个字符串小结

个人信息:就读于燕大本科软件工程专业 目前大三;

本人博客:google搜索“cqs_2012”即可;

个人爱好:酷爱数据结构和算法,希望将来搞科研为人民作出自己的贡献;

博客内容:两个字符串;

博客时间:2014-4-2

编程语言:C++

编程坏境:Windows

编程工具:vs2008

 

  • 引言

我还是那个小时候的我,真好。环境没有扼杀我。

  • 题目

关于两个字符串的问题有很多,下面我将为大家总结三个

1.检查一个字符串是否包含另一个字符串

2.找出两个字符串的最长公共子串

3.找出两个字符串的最多的公共元素

  • 思路

第一题解法:这是一个很经典的问题,我的知识源自算法圣经 KMP算法,详解请戳个人博客 KMP算法讲解

第二题解法:这个问题选自面试题,很好,我想到的是DP算法

建立一个表就可以,见表算法如下

tongji【i】【j】 存的是 a.substr(0,j-1) 和 b.substr( 0 , i-1  )  这两个字符串的最长的公共字串的长度

在程序中如下

 

		// go on algorithm
		int max_i = 0, max_j = 0 ;
		for(size_t i =1; i <= b.length();i++)
		{
			for(size_t j = 1;j <= a.length();j++)
			{
				if( b[i-1] == a[j-1] )
				{
					tongji[i][j] = tongji[i-1][j-1] + 1 ;
					if(tongji[i][j] > tongji[max_i][max_j])
					{
						max_i = i ;
						max_j = j ;
					}
				}
				else tongji[i][j] = 0 ;
			}
		}


第三题解法:问题选自面试题,我的解法是DP解法

建立两个表,表1

tongji【i】【j】 存放的是 a.substr(0,j-1) 和 b.substr( 0 , i-1  )  这两个字符串的最多公共字符的个数

dp_table【i】【j】 存放的是 a【j】  和 b【i 】 这两个字符关系而导致的子问题答案取向 ,如下

如下

 

 

代码算法如下

		// go on algorithm
		for(size_t i =1; i <= b.length();i++)
		{
			for(size_t j = 1;j <= a.length();j++)
			{
				if( b[i-1] == a[j-1] )
				{
					tongji[i][j] = _get_max_three(tongji[i-1][j],tongji[i-1][j-1]+1,tongji[i][j-1],dp_table[i-1][j-1]);
				}
				else tongji[i][j] = _get_max_two(tongji[i-1][j],tongji[i][j-1],dp_table[i-1][j-1]);
			}
		}
  • 实验

第一题很抱歉:程序目前没实现,稍后给出,请见谅

第二题实验:

 

程序说明: 第一行和第二行是输入的两个字符串

第三行输出 DP建的表

第四行输出 一个答案(这个题目其实有个答案,我只给出了一个as,可以有很多个)

第三题实验

程序解释

第一行和第二行输入 两个字符串

第三部分 输出 DP建的一个表,另一个表没有给

第四行输出 结果

  • 代码

第一题稍后给出

第二题如下:

test.cpp

// head file
#include<iostream>
#include<string>
using namespace std;

// function: longest common substring of string a and b
// input: two string a and b
// output a string
// 功能: 求出两个字符串的
string _Longest_common_substr(string a,string b);



// function: longest common substring of string a and b
// input: two string a and b
// output a string
// 功能: 求出两个字符串的最长公共字串
string _Longest_common_substr(string a,string b)
{
	if(a.empty() || b.empty())
	{
		cout<<"exception of function Longest_common_substr input"<<endl;
		return "error";
	}
	else
	{
		// make space for table
		int ** tongji = new int*[b.length()+1];
		for(size_t i = 0;i<= b.length();i++)
		{
			tongji[i] = new int[a.length()+1];
		}

		// data initialize
		for(size_t i = 0;i <= b.length();i++)
		{
			tongji[i][0] = 0;
		}
		for(size_t i = 1;i <= a.length();i++)
		{
			tongji[0][i] = 0;
		}

		// go on algorithm
		int max_i = 0, max_j = 0 ;
		for(size_t i =1; i <= b.length();i++)
		{
			for(size_t j = 1;j <= a.length();j++)
			{
				if( b[i-1] == a[j-1] )
				{
					tongji[i][j] = tongji[i-1][j-1] + 1 ;
					if(tongji[i][j] > tongji[max_i][max_j])
					{
						max_i = i ;
						max_j = j ;
					}
				}
				else tongji[i][j] = 0 ;
			}
		}

		string result = "";
		while( tongji[max_i][max_j] > 0)
		{
			result = b[max_i-1] + result ;
			max_i --;
			max_j --;
		}
		// output table
		for(size_t i =1; i <= b.length();i++)
		{
			for(size_t j = 1;j <= a.length();j++)
			{
				cout<<tongji[i][j]<<" ";
			}
			cout<<endl;
		}
		return result ;
	}
}

// function: main
int main()
{
	string a,b;
	while(cin>>a>>b)
		cout<<_Longest_common_substr(a,b)<<endl;

	system("pause");
	return 0;
}


第三题代码如下

test.cpp

// head file
#include<iostream>
#include<string>
using namespace std;




// function : get the max number from three number
int _get_max_three(int a,int b,int c,char & d);

// function : get the max number from two number
int _get_max_two(int a,int b,char & c);

// function: Most common elems
// input: two string a and b
// output a string
// 功能: 求出两个字符串的最多公有字符
string _Most_common_char(string a,string b);

// function: main
int main()
{
	string a,b;
	while(cin>>a>>b)
		cout<<_Most_common_char(a,b)<<endl;

	system("pause");
	return 0;
}




// function: Most common elems
// input: two string a and b
// output a string
// 功能: 求出两个字符串的最多公有字符
string _Most_common_char(string a,string b)
{
	if(a.empty() || b.empty())
	{
		cout<<"exception of function Longest_common_substr input"<<endl;
		return "error";
	}
	else
	{
		// make space for table
		int ** tongji = new int*[b.length()+1];
		char ** dp_table = new char*[b.length()];
		for(size_t i = 0;i<= b.length();i++)
		{
			tongji[i] = new int[a.length()+1];
			dp_table[i] = new char[a.length()];
		}

		// data initialize
		for(size_t i = 0;i <= b.length();i++)
		{
			tongji[i][0] = 0;
		}
		for(size_t i = 1;i <= a.length();i++)
		{
			tongji[0][i] = 0;
		}

		// go on algorithm
		for(size_t i =1; i <= b.length();i++)
		{
			for(size_t j = 1;j <= a.length();j++)
			{
				if( b[i-1] == a[j-1] )
				{
					tongji[i][j] = _get_max_three(tongji[i-1][j],tongji[i-1][j-1]+1,tongji[i][j-1],dp_table[i-1][j-1]);
				}
				else tongji[i][j] = _get_max_two(tongji[i-1][j],tongji[i][j-1],dp_table[i-1][j-1]);
			}
		}
		

		// output the table
		for(size_t i = 0;i < b.length();i++)
		{
			for(size_t j = 0; j < a.length(); j++)
			{
				cout<<dp_table[i][j]<<" ";
			}
			cout<<endl;
		}

		// get the result
		int max_i  = b.length() -1,max_j = a.length()-1;
		string result = "";
		while( max_i >= 0 && max_j >= 0 )
		{
			switch( dp_table[max_i][max_j] ) 
			{
				case '|':max_i--;break;
				case '\\':result = b[max_i] + result;max_i--;max_j--;break;
				case '-':max_j--;break;
			}
		}
		return result;
	}
}

// function : get the max number from three number
int _get_max_three(int a,int b,int c,char & d)
{
	if(a > b)
	{
		if(a > c)
		{
			d = '|';
			return a;
		}
		else 
		{
			d = '-';
			return c;
		}
	}
	else
	{
		if( b>c )
		{
			d ='\\';
			return b;
		}
		else
		{
			d = '-';
			return c;
		}
	}
}

// function : get the max number from two number
int _get_max_two(int a,int b,char & c)
{
	if(a>b)
	{
		c = '|';
		return a;
	}
	else
	{
		c = '-';
		return b;
	}
}


 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值