zoukankan      html  css  js  c++  java
  • 基础算法一

    字符串匹配

     1 //字符串匹配
     2 #include<iostream>
     3 #include<windowsx.h>
     4 #include<string.h>
     5 using namespace std;
     6 
     7 int ViolentMatch(char *s, char *t)
     8 {
     9     int slen = strlen(s);
    10     int tlen = strlen(t);
    11     int i = 0,j = 0;
    12     while (i < slen && j< tlen)
    13     {
    14         if (s[i] == t[j])
    15         {
    16             i++;
    17             j++;
    18         }
    19         else
    20         {
    21             i = i - j + 1;
    22             j = 0;
    23         }
    24     }
    25     if (j == tlen)
    26     {
    27         return i - j;
    28     }
    29     else
    30     {
    31         return -1; 
    32     }
    33 
    34 }
    35 
    36 int  main()
    37 {
    38     //char *a="aaaabaa" ; char *b="b" ;
    39     string a;string b;
    40     
    41 
    42     cout << ViolentMatch(a, b) << endl;
    43     system("pause");
    44     return 0;
    View Code

    最大回文子串

     1 //最大回文子串
     2 /*
     3 #include<iostream>
     4 
     5 using namespace std;
     6 
     7 int LongestPalinderom(char *s, int n)
     8 {
     9     int i, j, max, c;
    10     if (s == 0 || n < 0)
    11     {
    12         return 0;
    13     }
    14     max = 0;
    15     for (i = 0;i < n;++i)
    16     {
    17         for (j = 0;(i - j >= 0) && (i + j < n);++j)
    18         {
    19             if (s[i - j] != s[i + j])
    20             {
    21                 break;
    22             }
    23             c = j * 2 + 1;//对称字符串为奇数
    24         }
    25         if (c > max)
    26         {
    27             max = c;
    28         }
    29         for (j = 0;(i - j >= 0) && (i + j + 1 < n);++j)
    30         {
    31             if (s[i - j] != s[i + j + 1])
    32             {
    33                 break;
    34             }
    35             c = j * 2 + 2;//对称字符串为偶数
    36         }
    37         if (c > max)
    38         {
    39             max = c;
    40         }
    41     }
    42     return max;
    43 }
    44 
    45 int main()
    46 {
    47     char a[64];
    48     int n;
    49     cin >> a;
    50     n=strlen(a);
    51     cout << LongestPalinderom(a, n) << endl;
    52     system("pause");
    53     return 0;    
    54 }
    55 
    56 */
    View Code

    全排列

     1 //全排列
     2 /*
     3 #include<iostream>  
     4 using namespace std;
     5 #include<assert.h>  
     6 
     7 //在[nBegin,nEnd)区间中是否有字符与下标为pEnd的字符相等  
     8 bool IsSwap(char* pBegin, char* pEnd)
     9 {
    10     char *p;
    11     for (p = pBegin; p < pEnd; p++)
    12     {
    13         if (*p == *pEnd)
    14             return false;
    15     }
    16     return true;
    17 }
    18 void Permutation(char* pStr, char *pBegin)
    19 {
    20     assert(pStr);
    21 
    22     if (*pBegin == '')
    23     {
    24         static int num = 1;  //局部静态变量,用来统计全排列的个数  
    25         printf("第%d个排列	%s
    ", num++, pStr);
    26     }
    27     else
    28     {
    29         for (char *pCh = pBegin; *pCh != ''; pCh++)   //第pBegin个数分别与它后面的数字交换就能得到新的排列     
    30         {
    31             if (IsSwap(pBegin, pCh))
    32             {
    33                 swap(*pBegin, *pCh);
    34                 Permutation(pStr, pBegin + 1);
    35                 swap(*pBegin, *pCh);
    36             }
    37         }
    38     }
    39 }
    40 
    41 int main(void)
    42 {
    43     char str[64];
    44     //    int n;
    45     cin >> str;
    46     //    n=strlen(a)-1; 
    47     // char str[] = "baa";  
    48     Permutation(str, str);
    49     system("pause");
    50     return 0;
    51 }
    52 
    53 */
    View Code

    汉诺塔

     1 //汉诺塔
     2 
     3 #include<iostream>
     4 
     5 using namespace std;
     6 
     7 void hanoi(int n, char one, char two, char three)//n个盘子从one移到three(借助two)
     8 {
     9     int count = 0;
    10     void move(char x, char y);
    11     if (n == 1)
    12         move(one, three);
    13     else
    14     {
    15         hanoi(n - 1, one, three, two);//n-1个盘子从one移到two
    16         move(one, three);
    17         hanoi(n - 1, two, one, three);//n-1个盘子在从two移到three
    18     }
    19 }
    20 
    21 void move(char x, char y)
    22 {
    23     cout << x << "-->" << y << endl;
    24 }
    25 
    26 int main()
    27 {
    28     void hanoi(int n, char one, char two, char three);
    29     int m;      //输入多少个盘子
    30     cin >> m;
    31     hanoi(m, 'A', 'B', 'C');
    32     system("pause");
    33     return 0;
    34 }
    View Code

     

    反转I am a boy 为boy a am I 

     1 //反转I am a boy 为boy a am I 
     2 /*
     3 #include<iostream>
     4 #include <string.h>
     5 #include <cstdio>
     6 using namespace std;
     7 
     8 void RS(char*bp, char *ep)
     9 {
    10     while (bp<ep)
    11     {
    12         //char temp = *bp;
    13         //*bp = *ep;
    14         //*ep = temp;
    15         
    16         swap(*ep, *bp);
    17         bp++;
    18         ep--;
    19     }
    20 }
    21 char *Reverse(char *s)
    22 {
    23     int len = strlen(s);
    24     char *es = s + len - 1;
    25     RS(s, es);
    26 
    27     char *p1 = s;
    28     char *p2 = s;
    29     while (*p2 != '')
    30     {
    31         while (*p2 != '' && *p2 != ' ')
    32         {
    33             p2++;
    34         }
    35 
    36         RS(p1, p2 - 1);
    37         if (*p2 == ' '&& *p2 != '')
    38         {
    39             p2++;
    40             p1 = p2;
    41         }
    42     }
    43     return s;
    44 }
    45 int main()
    46 {
    47     char str[500];
    48     //getchar();
    49     gets_s(str);
    50     cout << Reverse(str) << endl;
    51     system("pause");
    52     return 0;
    53 }
    54 */
    View Code

    转载请说明出处!
  • 相关阅读:
    winform+c#之窗体之间的传值 Virus
    ASP.NET 2.0 利用 checkbox获得选中行的行号, 在footer中显示 Virus
    .NET中的winform的listview控件 Virus
    我的书橱
    Expert .NET 2.0 IL Assembler·译者序一 写在一稿完成之即
    Verbal Description of Custom Attribute Value
    AddressOfCallBacks in TLS
    下一阶段Schedule
    2008 Oct MVP OpenDay 第二天 博客园聚会
    2008 Oct MVP OpenDay 第二天 颁奖·讲座·晚会
  • 原文地址:https://www.cnblogs.com/zengshangzhi/p/8866831.html
Copyright © 2011-2022 走看看