分类: 2.数据结构与算法2011-08-14 14:27 9595人阅读 评论(15) 收藏 举报

<以下微软面试题全来自网络>

<以下答案与分析纯属个人观点,不足之处,还望不吝指出^_^>

<出处:http://blog.csdn.net/zhanxinhang>

:1、如何判断一个字符串是对称的?如a,aa,aba。

2、如何利用2函数找出一个字符串中的所有对称子串?

分析:

看第一个问题判断字符串对称,有以下两种方法。

方法一、使迭代器p1指向头,p2指向尾。使p1,p2相对而行(—> | <),每次比较p1,p2是否相等,直到它们相遇。

方法二、使p1、p2指向中间的一个元素(如果字符串长度为偶数的话就指向中间两个相邻的元素),使它们相向而行(<— |—>),每次比较p1,p2是否相等。直到它们一个指向头一个指向尾便结束。

(可以看出方法1明显好于方法2)

在看第二个问题打印所有的对称子串,判断对称子串的问题我们似乎已经解决,且有以上两种方法,针对现在的情况是否两种都合适呢,确切的说不是。如果是方法一,请想象一下,如果要p1,p2一个指向子串的头一个指向子串的尾,那么至少要两层循环一层控制p1一层控制p2,还有一层循环用于判断子串是否对称,也就是说总共需要三层嵌套循环,时间复杂度指数为3。而如果选择方法二呢? 两层循环就能实现,不过要适应现在这种情况需要做些修改,就是针对偶数长度的子串进行一次遍历,再针对奇数长度的子串进行一次遍历,也就是两层嵌套循环中内层有两次循环,时间复杂度指数为2。详情且看代码。

实现加测试代码:

  1. /**
  2. Author:花心龟
  3. Blog:http://blog.csdn.net/zhanxinhang
  4. **/
  5. #include <iostream>
  6. #include <cstdlib>
  7. #include <ctime>
  8. using namespace std;
  9. class App
  10. {
  11. typedef string::iterator Iter_t;
  12. string m_str;
  13. void print(Iter_t p1, Iter_t p2) //打印从p1开始到p2结束的字符
  14. {
  15. while(p1<=p2)
  16. {
  17. cout<<*p1;
  18. p1++;
  19. }
  20. cout<<" | ";
  21. }
  22. bool isHuiwen1(Iter_t start,Iter_t end) //法1判断是否为对称子串
  23. {
  24. Iter_t p1 = start;
  25. Iter_t p2 = end;
  26. int times = (distance(p1,p2)+1) / 2; //比较次数
  27. while(times)
  28. {
  29. if(*p1 != *p2)
  30. return false;
  31. p1++;
  32. p2--;
  33. times--;
  34. }
  35. return true;
  36. }
  37. bool isHuiwen2(Iter_t mid_it) //法2判断是否为对称子串
  38. {
  39. Iter_t p1,p2;
  40. p1 = p2 = mid_it;
  41. while(p1>=m_str.begin() && p2 < m_str.end())
  42. {
  43. if(*p1 != *p2)
  44. break;
  45. print(p1,p2);//打印从p1到p2的字符
  46. p1--,p2++;
  47. }
  48. p1 = p2 = mid_it;
  49. p2++; //使p2向前移动一个位置,此时p1,p2分别指向中间两个相邻位置
  50. while(p1>=m_str.begin() && p2 < m_str.end())
  51. {
  52. if(*p1 != *p2)
  53. return false;
  54. print(p1,p2);//打印从p1到p2的字符
  55. p1--,p2++;
  56. }
  57. return true;
  58. }
  59. void show_all_substr_of_huiwen1() //法一打印所有对称子串
  60. {
  61. Iter_t p2=m_str.end()-1;
  62. Iter_t p1 = m_str.begin();
  63. for(;p1!=m_str.end();p1++)
  64. for(p2=p1;p2!=m_str.end();p2++)
  65. {
  66. if(isHuiwen1(p1,p2))
  67. print(p1,p2);
  68. }
  69. }
  70. void show_all_substr_of_huiwen2()  //法二打印所有对称子串
  71. {
  72. Iter_t it = m_str.begin();
  73. for(;it!=m_str.end();it++)
  74. {
  75. isHuiwen2(it);
  76. }
  77. }
  78. public:
  79. void run()
  80. {
  81. m_str="abaaba";
  82. cout<<"测试数据为:abaaba"<<endl;
  83. show_all_substr_of_huiwen1();
  84. cout<<endl;
  85. show_all_substr_of_huiwen2();
  86. cout<<endl;
  87. m_str="asdfie";
  88. cout<<"测试数据为:asdfie"<<endl;
  89. show_all_substr_of_huiwen1();
  90. cout<<endl;
  91. show_all_substr_of_huiwen2();
  92. cout<<endl;
  93. m_str="aabaa";
  94. cout<<"测试数据为:aabaa"<<endl;
  95. show_all_substr_of_huiwen1();
  96. cout<<endl;
  97. show_all_substr_of_huiwen2();
  98. cout<<endl;
  99. //时间比较//
  100. m_str="this is a string for testing. aabaa alskjdfkljasdjflasdflkajsldkjfsjlakjsdlfjwoekjlakjlsdkjflsajlkdjfowieuoriuq aaddbb sldjfalkjsdlfkjasldjflajsldfjalskdjflakjsdlfkjaslkdjflajsdlfkjaslkdjflkajsdlkjflkasjdlfjaklsjdkfljaklsdjfklsajdflkjslkdjflaskjdlfkjalsdjlfkajsldfkjlaksjdfljasldjflaskjdfkasjdflaksjdkfljaskldfjlaksjdfljasldjflaksjdkljfkalsjdlkfjasldjflasjdlfjasldjfklsajdfljaskldfjlsakjdflkasjdfkl this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.make -k make -k make -k make -k make -k make -k is is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.make -k make -k make -k make -k make -k make -k is is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.make -k make -k make -k make -k make -k make -k is is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.make -k make -k make -k make -k make -k make -k is is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.make -k make -k make -k make -k make -k make -k is is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.make -k make -k make -k make -k make -k make -k is is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.make -k make -k make -k make -k make -k make -k is is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.make -k make -k make -k make -k make -k make -k is is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.make -k make -k make -k make -k make -k make -k is is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.make -k make -k make -k make -k make -k make -k is is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.make -k make -k make -k make -k make -k make -k is is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.this is a string for testing.make -k make -k make -k make -k make -k make -k end";
  101. time_t start,record1;
  102. cout<<"show all substr of huiwen 1:";
  103. system("pause");
  104. start = time(NULL);
  105. show_all_substr_of_huiwen1();
  106. record1 = time(NULL);
  107. cout<<endl;
  108. cout<<"time:"<<record1-start;
  109. cout<<endl;
  110. cout<<"show all substr of huiwen 2:";
  111. system("pause");
  112. start = time(NULL);
  113. show_all_substr_of_huiwen2();
  114. record1 = time(NULL);
  115. cout<<endl;
  116. cout<<"time:"<<record1-start;
  117. cout<<endl;
  118. cout<<"(可以看到打印速度明显快于上一次)";
  119. }
  120. };
  121. int main()
  122. {
  123. App myapp;
  124. myapp.run();
  125. system("pause");
  126. return 0;
  127. }

测试结果:

各测试数据下的一二行为打印出来的所有对称字串。

按下任意键后:

以上是使用方法1打印出来的结果。

按下任意键后:

以上便是用方法2打印出来的结果。

每日微软面试题——day 6(打印所有对称子串)的更多相关文章

  1. 每日三道面试题,通往自由的道路6——JVM

    茫茫人海千千万万,感谢这一秒你看到这里.希望我的面试题系列能对你的有所帮助!共勉! 愿你在未来的日子,保持热爱,奔赴山海! 每日三道面试题,成就更好自我 今天我们继续聊聊JVM的话题吧! 1. 那你知 ...

  2. 《剑指offer》面试题12:打印1到最大的n位数

    面试题12:打印1到最大的n位数 剑指offer题目12,题目如下 输入数字n,按顺序打印出1到最大的n位十进制数,比如输入3,则打印出1,2,3一直到最大的三位数999 方法一 和面试题11< ...

  3. 【面试题012】打印1到最大的n位数

    [面试题012]打印1到最大的n位数  大数问题 字符串中的每一个字符都是‘0’到‘9’之间的某一个字符,用来表示数字中的一位,因为数字最大是n位的,因此我们需要一个长度为n+1的字符串,字符串的最后 ...

  4. 【剑指offer】面试题 29. 顺时针打印矩阵

    面试题 29. 顺时针打印矩阵 题目描述 题目:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...

  5. 面试题17:打印1到最大的n位数

    // 面试题17:打印1到最大的n位数 // 题目:输入数字n,按顺序打印出从1最大的n位十进制数.比如输入3,则 // 打印出1.2.3一直到最大的3位数即999. 解题思路: 首先是一个大陷阱,n ...

  6. 每日三道面试题,通往自由的道路5——JVM

    茫茫人海千千万万,感谢这一秒你看到这里.希望我的面试题系列能对你的有所帮助!共勉! 愿你在未来的日子,保持热爱,奔赴山海! 每日三道面试题,成就更好自我 昨天既然我们聊到了JVM,那我们继续这一个话题 ...

  7. 每日三道面试题,通往自由的道路4——JVM篇

    茫茫人海千千万万,感谢这一秒你看到这里.希望我的面试题系列能对你的有所帮助!共勉! 愿你在未来的日子,保持热爱,奔赴山海! 每日三道面试题,成就更好自我 昨天既然你有讲到字符串常量池是吧,那这样吧 1 ...

  8. 每日三道面试题,通往自由的道路10——JMM篇

    茫茫人海千千万万,感谢这一秒你看到这里.希望我的面试题系列能对你的有所帮助!共勉! 愿你在未来的日子,保持热爱,奔赴山海! 每日三道面试题,成就更好自我 今天我们还是继续聊聊多线程的一些其他话题吧! ...

  9. 每日三道面试题,通往自由的道路13——锁+Volatile

    茫茫人海千千万万,感谢这一秒你看到这里.希望我的面试题系列能对你的有所帮助!共勉! 愿你在未来的日子,保持热爱,奔赴山海! 每日三道面试题,成就更好自我 我们既然聊到了并发多线程的问题,怎么能少得了锁 ...

随机推荐

  1. 二十七(序幕)、【开源】EFW框架破茧成蝶

    回<[开源]EFW框架系列文章索引>        EFW框架源代码下载V1.3:http://pan.baidu.com/s/1c0dADO0 EFW框架实例源代码下载:http://p ...

  2. 在linux安装mysql,并设置远程访问

    1.查看系统有没有安装mysql. vpm -qa mysql 发现有删除:rpm -e mysql(rpm -e --nodeps mysql) 2.下载数据库 mysql-standard-5.0 ...

  3. php随机密码函数的实例代码

    php随机密码函数的入门例子 时间:2015-12-16 20:42:48来源:网络 导读:php生成随机密码的函数实例,php生成随机密码的函数,生成数字.大小写字母与特殊字符组合的随机密码.   ...

  4. Legolas工业自动化平台入门(二)数据响应动作

    在上一篇文章Legolas工业自动化平台入门(一)搭建应用里,我们简单地提到了"动作"的概念.不清楚的童鞋不要着急,这篇我们就来介绍如何在Legolas平台上添加动作,并应用动作. ...

  5. 【NS2仿真】TCP协议

    # # ftp # \ # tcp sink # \ / # n0--------5M 2ms---------n1 # # set ns [new Simulator] set f [open ou ...

  6. 基于jQuery点击加载动画按钮特效

    分享一款基于jQuery点击加载动画按钮特效.这是一款基于jQuery+CSS3实现的鼠标点击按钮加载动画特效代码.效果图如下: 在线预览   源码下载 实现的代码. html代码: <div ...

  7. 各种Camera,总有一款适合你(二)

    在实际的项目开发中,一般需要程序抽象出一些在几何意义上有明确意义的参数,这样方便策划或美术在自己的机器上进行调试. 下面是一个可变参的地下城摄像机的简单实现: // 第三人称摄像机,平移和旋转会同时进 ...

  8. IOS学习之路七(使用 Operation 异步运行任务)

    在 application delegate 头文件(.h)中声明一个 operation 队列和两个 invocation operations: #import <UIKit/UIKit.h ...

  9. 【原创】C#搭建足球赛事资料库与预测平台(4) 比赛信息数据表设计

            本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新 开源C#彩票数据资料库系列文章总目录:[目录]C#搭建足球赛事资料库与预测平台与彩票数据分析目录 本篇文章开始将逐步介 ...

  10. [SLAM] Studying Guidance

    Books from Zhihu: 幽默一把 看完Gonzalez:嗯,好像很好玩的样子,我也来搞一搞.看完Price:什么鬼,怎么这么多公式,公式看不懂肿么破.看完Szeliski:原来用一千页的书 ...