一些简单数据结构算法的实现4

来源:互联网 发布:促销软件 编辑:程序博客网 时间:2024/05/20 23:02

http://zhedahht.blog.163.com/blog/static/25411174201063105120425/

  一个字符串中最长的对称字符字串,如google的最长对称字串为goog,长度为4

 

 

O(n3) 时间复杂度算法

#include<iostream>

usingnamespace std;

bool IsSym(char *start,char *end) {

       if (start == NULL || end == NULL || start> end)

              returnfalse;

       while (start < end) {

              if (*start !=*end)

                     returnfalse;

              start++;

              end--;

       }

       returntrue;

}

int getLongestString(char *pStr) {

       if (pStr == NULL)

              return 0;

       int longest = 1;

 

       char *pStart = pStr;

       int length =strlen(pStr);

       char *pEnd = &pStr[length - 1];

       int len = 0;

 

       while (pStart < pEnd) {

              char *pLast = pStart+ 1;

              while (pLast <=pEnd) {

                     if (IsSym(pStart,pLast)) {

                            len =pLast - pStart + 1;

                            if (len >longest) {

                                   longest= len;

                            }

                     }

                     pLast++;

              }

              pStart++;

       }

       return longest;

}

int main() {

       char *s ="googleelgooeeee";

       cout <<getLongestString(s) << endl;

       return 0;

}

 

 

时间复杂度为o(n2)的算法

 

#include<iostream>

usingnamespace std;

int longest;

void getLongestString(char *str) {

 

       if(str==NULL)

              return ;

       char *l1 = str;

       char *l2 =str;

       longest = 1;

       char *ch = str;

       char *start = NULL;

       char *last = NULL;

       int length = 0;

       while (*ch !='\0') {

              start = ch - 1;

              last = ch + 1;

              while (start >=str && *last !='\0' && *start == *last) {

                     start--;

                     last++;

              }

              length = last - start -1;

              if (length >longest) {

                     longest =length;

                     l1 = start + 1;

                     l2 = last - 1;

              }

 

              start = ch;

              last = ch + 1;

              while (start >=str && *last !='\0' && *start == *last) {

                     start--;

                     last++;

              }

              length = last - start -1;

              if (length >longest) {

                     longest =length;

                     l1 = start + 1;

                     l2 = last - 1;

              }

              ch++;

       }

       cout<<"最长对称字串长度为:"<<longest<<endl;

 

       while(l1<=l2)

       {

              cout<<*l1;

              l1++;

       }

       cout<<endl;

 

}

 

       int main() {

              char *s ="googleelgooaaa";

              getLongestString(s);

              return 0;

       }

 

 

人民币的问题

题目:人民币有1元、2元、5元、10元、20元、50元、100元面值,试给出一个算法找出和为100的人民币组合(不包括100本身)。比如2张50的,或者2张50+2张20+1张10等。

分析:该题目与上面问题类似,可以采用组合的思路来解决。代码如下:

[cpp] view plaincopy

1.     /* 

2.      * rmb.cpp 

3.      * 

4.      *  Created on: 2012-8-28 

5.      *      Author: shusheng 

6.      */  

7.       

8.     #include <iostream>  

9.     using namespace std;  

10.     

11.   #define N 6  

12.     

13.   int w[N];  

14.   int number_used[N];  

15.   bool is_used[N];  

16.   int countnum = 0;  

17.     

18.   void init()   

19.   {  

20.       w[0] = 1;  

21.       w[1] = 2;  

22.       w[2] = 5;  

23.       w[3] = 10;  

24.       w[4] = 20;  

25.       w[5] = 50;  

26.       for (int i = 0; i < N; i++) {  

27.           number_used[i] = 0;  

28.       }  

29.   }  

30.     

31.   void rmb(int start_index, int left_weight)   

32.   {  

33.       if (left_weight == 0) {  

34.           for (int i = 0; i < N; i++) {  

35.               if (number_used[i] > 0)  

36.                   cout << w[i] << ": " << number_used[i] << " ";  

37.           }  

38.           cout << endl;  

39.           countnum++;  

40.           return;  

41.       }  

42.      for(inti=start_index;i<N;i++)

43.       {

44.         int y=left_weight;

45.         While(y>=w[i])

46.         {

47.          number_used[i]++;

48.          y-=w[i];

49.          rmb(i+1,left_weight-w[i]);

50.        }

51.       }

52.   }  

53.     

54.   int main()   

55.   {  

56.       init();  

57.       rmb(0, 100);  

58.       cout << countnum << endl;  

59.       return 0;  

60.   }  

 

输出序列和为某值的全部序列,如10  有序列  1 2 3 4 和 10

#include<iostream>

usingnamespace std;

void sum(intnum)

{

       if(num==1)

       {

              cout<<num<<endl;

              return ;

       }

       int first=1;

       int end=2;

       int sum=(first+end);

       while(first<=num)

       {

              if(sum<num)

              {

                     end++;

                     sum+=end;

              }

              elseif(sum>num)

              {

                     sum-=first;

                     first++;

              }elseif(sum==num)

              {

                     cout<<"和等于"<<num<<"的数字序列有:";

                     for(inti=first;i<=end;i++)

                            cout<<i<<" ";

                     cout<<endl;

 

                     sum-=first;

                     first++;

              }

       }

}

int main()

{

 

       int num;

       while(cin>>num&&num)

       {

              sum(num);

       }

       return 0;

}


两个字符串,写一个函数实现 str1中包含str2的起始位置

 

  最长公共子序列

//============================================================================

// Name        :Juzhen.cpp

// Author      :andy

// Version     :

// Copyright   :Your copyright notice

// Description : Hello World in C++,Ansi-style

//============================================================================

 

#include<iostream>

usingnamespace std;

char a[100];

char b[100];

int c[101][101];

int map[101][101];

void printSeq(int x,int y)

{

   if(x==0||y==0)

   {

          cout<<endl;

          return ;

   }

   if(map[x][y]==0)

   {

          printSeq(x-1,y-1);

          cout<<a[x-1];

   }else if(map[x][y]==1)

   {

          printSeq(x-1,y);

   }else if(map[x][y]==-1)

   {

          printSeq(x,y-1);

   }

}

void printLcs()

{

   int n=strlen(a);

   int m=strlen(b);

   for(int i=0;i<=n;i++)

          c[i][0]=0;

   for(int i=0;i<=m;i++)

          c[0][i]=0;

   for(int i=1;i<=n;i++)

   {

          for(int j=1;j<=m;j++)

          {

                 if(a[i-1]==b[j-1])

                 {

                        c[i][j]=c[i-1][j-1]+1;

                        map[i][j]=0;

                 }elseif(c[i-1][j]>=c[i][j-1])

                 {

                        c[i][j]=c[i-1][j];

                        map[i][j]=1;

                 }else

                 {

                        c[i][j]=c[i-1][j];

                        map[i][j]=-1;

                 }

          }

   }

   cout<<"最长序列为:"<<c[n][m]<<endl;

   printSeq(n,m);

}

 

int main()

{

       while(cin>>a>>b)

       {

              printLcs();

       }

       return 0;

}

 

 

11.6日

 将一个数组中的奇数全部放到偶数的前面

#include<iostream>

usingnamespace std;

/**

 * 第一种partition的方法将后面的奇数换到最前面,奇数出现的顺序与原来相反了

 */

void evenPre(int *pData,int length)

{

       if(pData==NULL||length==0)

              return;

       int *pBegin=pData;

       int *pEnd=pData+length-1;

       while(pBegin<pEnd)

       {

              while(pBegin<pEnd&&(*pBegin&0x1)!=0) //找到第一个偶数

              {

                     pBegin++;

              }

              while(pBegin<pEnd&&(*pEnd&0x1)==0)

              {

                     pEnd--;

              }

              if(pBegin<pEnd)

              {

                     int temp=*pBegin;

                     *pBegin=*pEnd;

                     *pEnd=temp;

              }

       }

}

/**

 * 数字出现顺序与原来一致

 */

void evenPre2(int *pData,int length)

{

  if(pData==NULL||length==0)

         return ;

  int *p1=pData;

  int *p2=pData;

  int *pEnd=pData+length-1;

  while(p1<=pEnd&&p2<=pEnd)

  {

         if((*p1&0x1)==1)

         {

                int temp=*p1;

                *p1=*p2;

                *p2=temp;

                p1++;

                p2++;

         }else{

                p1++;

         }

  }

}

int main()

{

       int s[]={1,2,6,8,6,4,3,5,16,18,14,9};

       int len=sizeof(s)/sizeof(s[0]);

       evenPre2(s,len);

 

       for(int i=0;i<len;i++)

              cout<<s[i]<<" ";

       cout<<endl;

}

 

 

11.6 查找二维数组中最大和子矩阵

Eg:As an example, the maximalsub-rectangle of the array:

0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2

is in the lower left corner:

9 2
-4 1
-1 8

and has a sum of 15.

 

// 时间复杂度为o(n3)

算法的思想是基于一维的数组中,求最大和子序列开始的。

思考假设,最终求出的最大和子序列是,有i-j行的某几列组成的,那么把每一列看成一个元素,可以转换为求最大和子序列的方法。

总共有On2)个列的可能性,每个列和的计算需要on)的时间复杂度,所以总的时间复杂度是O(n3)

 

 

#include<iostream>

usingnamespace std;

int map[100][100];

int calVol(int col1,int col2, int vol) {

       int sum = 0;

       for (int i = col1; i <= col2; i++) {

              sum += map[i][vol];

       }

       return sum;

}

int maxSubMatrix(int n) {

       int max = INT_MIN;

       for (int i = 0; i < n; i++)

              for (int j = i; j <n; j++) {

                     int temp = 0;

                     for (int vol = 0; vol< n; vol++) {

                            temp +=calVol(i, j, vol);

                            if (temp < 0) {

                                   temp= 0;

                            } else {

                                   if (temp > max)

                                          max= temp;

                            }

                     }

              }

       return max;

}

int main() {

       int n;

       while (cin >> n && n) {

              memset(map, 0,sizeof(map));

              int m = n * n;

              for (int i = 0; i <n; i++)

                     for (int j = 0; j <n; j++)

                            cin >>map[i][j];

              for (int i = 0; i <n; i++) {

                     for (int j = 0; j <n; j++) {

                            cout<< map[i][j] <<" ";

                     }

                     cout <<endl;

              }

              cout <<maxSubMatrix(n) << endl;

       }

}

 

从某年开始,求第K个闰年

 

#include<iostream>

usingnamespace std;

int isRun(int x) {

       if ((x%4==0&&x%100!=0)||x%400==0)

              return 1;

       else

              return 0;

}

int main() {

       int n, num;

       int y;

       int target;

       cin >> n;

       while (n--) {

              int y;

              cin >> y >>num;

              if (isRun(y)) {

                     num--;

                     target = y;

              }else{

                     while(isRun(y)==0)

                     {

                            y++;

                     }

                     target=y;

                     num--;

              }

              while (num) {

                     target+=4;

                     if(isRun(target))

                     {

                            num--;

                     }

              }

              cout<<target<<endl;

       }

}

http://acm.hdu.edu.cn/showproblem.php?pid=2553

求N皇后问题

 

 

以下是完整的代码,但是时间超时

 

#include<iostream>

#include<cstring>

usingnamespace std;

 

int volIndex[100];

 

 

bool isCheck(int n) {

       bool flag =true;

       for (int i = 0; i < n; i++) {

              for (int j = i + 1; j< n; j++) {

                     if ((j - i) ==(volIndex[j] - volIndex[i]) || (i - j)

                                   ==(volIndex[j] - volIndex[i]))

                            flag = false;

              }

       }

       return flag;

}

void swap(int &a,int &b) {

       int temp = a;

       a = b;

       b = temp;

}

void permutation(int volIndex[],int n, int index,int &count) {

       if (index == n && isCheck(n) ) {

              count++;

       }

       for (int i = index; i < n; i++) {

              swap(volIndex[i],volIndex[index]);

              permutation(volIndex,n, index + 1,count);

              swap(volIndex[i],volIndex[index]);

       }

 

}

int QueueCount(int n) {

       int count=0;

       memset(volIndex, 0,sizeof(volIndex));

       for (int i = 0; i < n; i++)

              volIndex[i] = i;

       permutation(volIndex, n,0,count);

       return count;

}

int main() {

       int n;

       while (cin >> n && n) {

              cout <<QueueCount(n) << endl;

       }

       return 0;

}


原创粉丝点击