微软面试题day 6(打印所有对称子串)

来源:互联网 发布:网络靶场 中国 编辑:程序博客网 时间:2024/05/16 10:16

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

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


分析

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

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

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

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

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


实现加测试代码:

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

测试结果:

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

按下任意键后:

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

按下任意键后:

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


原创粉丝点击