前端程序员干货程序员计算机微刊

分治法与递归

2017-09-16  本文已影响70人  30fd099ab263

设计思想与策略


可以把原问题分解为已经解决的部分和未解决的部分,这两个部分的长度都有可能是0,对于已经解决的部分我们不去探讨,对于未解决的部分我们可以把其分解为若干个最小子问题的集合,对于每一个最小子问题我们都能简单求解。

分治法的适用条件

步骤和设计模式

设计模式



Divide-and-Conquer(P)
    1. if |P|≤n0
    2. then return(ADHOC(P))
    3. 将P分解为较小的子问题 P1 ,P2 ,...,Pk
    4. for i←1 to k
    5. do yi ← Divide-and-Conquer(Pi) △ 递归解决Pi
    6. T ← MERGE(y1,y2,...,yk) △ 合并子问题
    7. return(T)


思维过程

实际上就是类似于数学归纳法,找到解决本问题的求解方程公式,然后根据方程公式设计递归程序。

  1. 一定是先找到最小问题规模时的求解方法;
  1. 然后考虑随着问题规模增大时的求解方法;
  1. 找到求解的递归函数式后(各种规模或因子),设计递归程序即可。

Write function
void stringSplit (char *str, int n, int *length, ...);
which is able to split the string str into an undefined number of sub-strings whose sizes are stored in length[0], length[1], . . ., length[n-1]. Write an error message when it is not possible to split the string into sub-strings of those lengths. Print-out the generated sub-strings when it is possible.
For example, with:
• str = Hello, n = 2, and length = (2, 3), the program may produce sub-strings (He, llo), or (Hel, lo).
• str = Hello, n = 2, and length = (3, 4), there is no decomposition.
• str = sampleTest, n = 3, and length = (2, 3, 6), the program may produce sub-strings (sa, mp, le, Te, st), or sub-strings (sa, mp, leT, est), or sub-strings (sa, mpleTe, st), etc.



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 1024;

void stringSplit (
  char *str,
  int n,
  int *length
  )
{
  char *s;
  int i;

  s = (char *) malloc ((strlen (str) + 1) * sizeof (char));
  if (s==NULL) {
    fprintf (stderr, "Allocation Error.\n");
    exit (1);
  }
  for (i=0; i<strlen(str); i++) {
    s[i] = 0;
  }
  stringSplitRec (str, n, length, s, 0, 0);

  return;
}

int stringSplitRec (
  char *str,
  int n,
  int *length,
  char *res,
  int ind,
  int len
  )
{
  int i, j, k;

  // Termination with no solution
  if (len>strlen (str)) {
    return (0);
  }

  // Termination with a solution
  if (len==strlen (str)) {
    for (i=k=0; i<ind; i++) {
      for (j=0; j<res[i]; j++) {
        fprintf (stdout, "%c", str[k]);
        k++;
      }
      fprintf (stdout, " ");
    }
    fprintf (stdout, "\n");
    return (1);
  }

  // Try to use all lengths
  for (i=0; i<n; i++) {
    res[ind] = length[i];
#if 0
    /*
     *  Enable the following 2 lines
     *  to have ALL solutions
     */
    stringSplitRec (str, n, length, res, ind+1, len+length[i]);
#else
    /*
     *  Enable the following 2 lines
     *  to have only ONE solution
     */
    if (stringSplitRec (str, n, length, res, ind+1, len+length[i]))
      return (1);
#endif

  }

  return (0);
}

int main(int argc, char const *argv[]) {
  char str[1024];
  int n=0;
  int* length=(int*)malloc(sizeof(int)*n);
  if(length==NULL)
  {
    fprintf(stderr, "Allocation error\n" );
    exit(-1);
  }
  printf("input N and length vector\n" );
  scanf("%d",&n );
  for(int i=0;i<n;i++)
  {
    scanf("%d",&length[i] );
  }
  printf("string input\n" );
  scanf("%s",str);
  stringSplit(str,n,length);
  return 0;
}

上一篇下一篇

猜你喜欢

热点阅读