基于visual Studio2013解决面试题之1002公共子串
生活随笔
收集整理的這篇文章主要介紹了
基于visual Studio2013解决面试题之1002公共子串
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目
解決代碼及點評
/*求兩個字符串的最小公共子串這道題簡單的兩個字符串遍歷即可 */#include <iostream> using namespace std; const int N = 1000; char* go(char *str1,char *str2) { int maxindex; int maxlength = 0; int i,j; for(i=0;str1[i];i++) { for(j=0;str2[j];j++) { for(int k=0;str1[i+k]==str2[j+k] && (str1[i+k] || str2[i+k]);k++) if(k>maxlength) { maxindex = j; maxlength = k+1; } } } char *result = new char[maxlength+1]; for(i=0;i<maxlength;i++) result[i] = str2[maxindex++]; result[i] = '/0'; return result; } int main() { char *str1 = "abractyeyt"; char *str2 = "dgdsaeactyey"; cout<<go(str1,str2)<<endl;system("pause");return 0; }代碼下載及其運行
代碼下載地址:http://download.csdn.net/detail/yincheng01/6704519
解壓密碼:c.itcast.cn
下載代碼并解壓后,用VC2013打開interview.sln,并設置對應的啟動項目后,點擊運行即可,具體步驟如下:
1)設置啟動項目:右鍵點擊解決方案,在彈出菜單中選擇“設置啟動項目”
2)在下拉框中選擇相應項目,項目名和博客編號一致
3)點擊“本地Windows調試器”運行
程序運行結果
轉載于:https://www.cnblogs.com/niulanshan/p/6175112.html
總結
以上是生活随笔為你收集整理的基于visual Studio2013解决面试题之1002公共子串的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 运行级别脚本
- 下一篇: websocket在.net4.5中实现