面试题之两个字符串
2014-04-03 17:33
211 查看
两个字符串小结
个人信息:就读于燕大本科软件工程专业 目前大三;本人博客:google搜索“cqs_2012”即可;
个人爱好:酷爱数据结构和算法,希望将来搞科研为人民作出自己的贡献;
博客内容:两个字符串;
博客时间:2014-4-2
编程语言:C++
编程坏境:Windows
编程工具:vs2008
[align=left]引言[/align]
我还是那个小时候的我,真好。环境没有扼杀我。
[align=left]题目[/align]
关于两个字符串的问题有很多,下面我将为大家总结三个
1.检查一个字符串是否包含另一个字符串
2.找出两个字符串的最长公共子串
3.找出两个字符串的最多的公共元素
[align=left]思路[/align]
第一题解法:这是一个很经典的问题,我的知识源自算法圣经 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]); } }
[align=left]实验[/align]
第一题很抱歉:程序目前没实现,稍后给出,请见谅
第二题实验:
程序说明: 第一行和第二行是输入的两个字符串
第三行输出 DP建的表
第四行输出 一个答案(这个题目其实有个答案,我只给出了一个as,可以有很多个)
第三题实验
程序解释
第一行和第二行输入 两个字符串
第三部分 输出 DP建的一个表,另一个表没有给
第四行输出 结果
[align=left]代码[/align]
第一题稍后给出
第二题如下:
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; } }
相关文章推荐
- java面试题之 使用两个int翻转字符串
- 常见C语言面试题之五:两个字符串的最大公共子字符串
- 陈利人 面试题 给定两个字符串s和t(len(s)>len(t)),t可能是s的一个子序列。求所有s的子序列集合中,有多少个子序列等于t。
- 求职面试题(两个字符串是否包含相同的字符)
- 面试题:查找两个字符串的连续子串
- 面试题:两个字符串是变位词
- 面试题七 C/C++ 两个字符串由数字组成的相加,最大不超过32bit整形的系统函数--程序员面试题
- 求两个字符串最大公共字符串(听说是微软几年前面试题)
- Google 面试题 - 两个字符串的匹配问题(转)
- 网易面试题之 牛牛拿到了一个藏宝图,顺着藏宝图的指示,牛牛发现了一个藏宝盒,藏宝盒上有一个机关,机关每次会显示两个字符串 s 和 t, * 根据古老的传说,牛牛需要每次都回答 t 是否是 s 的子序
- 某公司的一道面试题:最新版本号;输入两个表示版本号的字符串,返回版本较大的一个
- 网易面试题之 牛牛拿到了一个藏宝图,顺着藏宝图的指示,牛牛发现了一个藏宝盒,藏宝盒上有一个机关,机关每次会显示两个字符串 s 和 t, * 根据古老的传说,牛牛需要每次都回答 t 是否是 s 的子序
- 剑指Offer面试题41和为s的两个数字与和为s的连续正数序列,面试题42翻转单词顺序与左旋转字符串
- 网易面试题之 牛牛拿到了一个藏宝图,顺着藏宝图的指示,牛牛发现了一个藏宝盒,藏宝盒上有一个机关,机关每次会显示两个字符串 s 和 t, * 根据古老的传说,牛牛需要每次都回答 t 是否是 s 的子序
- 网易面试题之 牛牛拿到了一个藏宝图,顺着藏宝图的指示,牛牛发现了一个藏宝盒,藏宝盒上有一个机关,机关每次会显示两个字符串 s 和 t, * 根据古老的传说,牛牛需要每次都回答 t 是否是 s 的子序
- 剑指Offer面试题49字符串转整数,面试题50二叉树两个结点的最低公共祖先
- 网易面试题之 牛牛拿到了一个藏宝图,顺着藏宝图的指示,牛牛发现了一个藏宝盒,藏宝盒上有一个机关,机关每次会显示两个字符串 s 和 t, * 根据古老的传说,牛牛需要每次都回答 t 是否是 s 的子序
- 网易面试题之 牛牛拿到了一个藏宝图,顺着藏宝图的指示,牛牛发现了一个藏宝盒,藏宝盒上有一个机关,机关每次会显示两个字符串 s 和 t, * 根据古老的传说,牛牛需要每次都回答 t 是否是 s 的子序
- [面试题]去除字符串中相邻两个字符的重复
- 面试题 输入两个很大的正数(用C字符串表示),输出它们的乘积,不考虑非法输入