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

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

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

 

 

O(n3) 时间复杂度算法

#include<iostream>

using namespace std;

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

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

              return false;

       while (start < end) {

              if (*start !=*end)

                     return false;

              start++;

              end--;

       }

       return true;

}

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>

using namespace 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>

using namespace std;

void sum(int num)

{

       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;

              }

              else if(sum>num)

              {

                     sum-=first;

                     first++;

              }else if(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>

using namespace 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;

                 }else if(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>

using namespace 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>

using namespace 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>

using namespace 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>

using namespace 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;

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
16进制10进制.txt 32.txt asm.txt Crctable.txt C标志符命名源程序.txt erre.txt erre2.txt ff.txt for循环的.txt list.log N皇后问题回溯算法.txt ping.txt re.txt source.txt winsock2.txt ww.txt 万年历.txt 万年历的算法 .txt 乘方函数桃子猴.txt 乘法矩阵.txt 二分查找1.txt 二分查找2.txt 二叉排序树.txt 二叉树.txt 二叉树实例.txt 二进制数.txt 二进制数2.txt 余弦曲线.txt 余弦直线.txt 傻瓜递归.txt 冒泡排序.txt 冒泡法改进.txt 动态计算网络最长最短路线.txt 十五人排序.txt 单循环链表.txt 单词倒转.txt 单链表.txt 单链表1.txt 单链表2.txt 单链表倒序.txt 单链表的处理全集.txt 双链表正排序.txt 反出字符.txt 叠代整除.txt 各种排序法.txt 哈夫曼算法.txt 哈慢树.txt 四分砝码.txt 四塔1.txt 四塔2.txt 回文.txt 图.txt 圆周率.txt 多位阶乘.txt 多位阶乘2.txt 大加数.txt 大小倍约.txt 大整数.txt 字符串查找.txt 字符编辑.txt 字符编辑技术(插入和删除) .txt 完数.txt 定长串.txt 实例1.txt 实例2.txt 实例3.txt 小写数字转换成大写数字1.txt 小写数字转换成大写数字2.txt 小写数字转换成大写数字3.txt 小字库DIY-.txt 小字库DIY.txt 小孩分糖果.txt 小明买书.txt 小白鼠钻迷宫.txt 带头结点双链循环线性表.txt 平方根.txt 建树和遍历.txt 建立链表1.txt 扫描码.txt 挽救软盘.txt 换位递归.txt 排序法.txt 推箱子.txt 数字移动.txt 数据结构.txt 数据结构2.txt 数据结构3.txt 数组完全单元.txt 数组操作.txt 数组递归退出.txt 数组递归退出2.txt 文件加密.txt 文件复制.txt 文件连接.txt 无向图.txt 时间陷阱.txt 杨辉三角形.txt 栈单元加.txt 栈操作.txt 桃子猴.txt 桶排序.txt 检出错误.txt 检测鼠标.txt 汉字字模.txt 汉诺塔.txt 汉诺塔2.txt 灯塔问题.txt 猴子和桃.txt 百鸡百钱.txt 矩阵乘法动态规划.txt 矩阵转换.txt 硬币分法.txt 神经元模型.txt 穷举搜索法.txt 符号图形.txt 简单数据库.txt 简单计算器.txt 简单逆阵.txt 线性顺序存储结构.txt 线索化二叉树.txt 绘制圆.txt 编随机数.txt 网络最短路径Dijkstra算法.txt 自我复制.txt 节点.txt 苹果分法.txt 螺旋数组1.txt 螺旋数组2.txt 试题.txt 诺汉塔画图版.txt 读写文本文件.txt 货郎担分枝限界图形演示.txt 货郎担限界算法.txt 质因子.txt 输出自已.txt 迷宫.txt 迷宫问题.txt 逆波兰计算器.txt 逆矩阵.txt 逆阵.txt 递堆法.txt 递归桃猴.txt 递归车厢.txt 递推.txt 逻辑移动.txt 链串.txt 链栈.txt 链表十五人排序.txt 链表(递归).txt 链队列.txt 队列.txt 阶乘递归.txt 阿姆斯特朗数.txt 非递归.txt 顺序栈.txt 顺序表.txt 顺序队列.txt 骑士遍历1.txt 骑士遍历2.txt 骑士遍历回逆.txt 黑白.txt

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值