user-0 提问: 二维数组中找最大特征数组

回复:
user: user-1
content: 51节前看到的，有个思路，趁机想把代码写下来。 可惜写了一点点下班了，摸鱼党用不加班。接着放51就没继续写了： 想法是： 010111101010100 111111100000111 011101010101011 101110100011110 011111110111011 找到1最多的前8列。设这8列的编号即为b[8]，当然，结果并不一定如此。获得此时这8列中1111 1111的数量n 我的想法是：如果这n值求得了。那么可以在后续的运算中排除1的数量小于n的列。因为这些列不可能存在b[8]中。 我觉得这个思路在特别多列的时候，又是纯随机的数据，效率提升会非常高【比老老实实算下去】

user: user-1
content: 51节前看到的，有个思路，趁机想把代码写下来。 可惜写了一点点下班了，摸鱼党用不加班。接着放51就没继续写了： 想法是： 010111101010100 111111100000111 011101010101011 101110100011110 011111110111011 找到1最多的前8列。设这8列的编号即为b[8]，当然，结果并不一定如此。获得此时这8列中1111 1111的数量n 我的想法是：如果这n值求得了。那么可以在后续的运算中排除1的数量小于n的列。因为这些列不可能存在b[8]中。 我觉得这个思路在特别多列的时候，又是纯随机的数据，效率提升会非常高【比老老实实算下去】

user: user-0
content: 引用 96 楼 早打大打打核战争 的回复:可以考虑把每行看作一个二进制数字，对按行排序过的矩阵再做处理？这样可能不需要C(5000, 8)次循环了，只需要循环5000次，每次处理5000、4999... 1行数据 具体怎么实现呢？

user: user-0
content: 是最多数量的行

user: user-2
content: b[8]的值不重复，找到第八个最多数量的列不就可以了？

user: user-0
content: 引用 92 楼 GKatHere 的回复:[quote=引用 86 楼 赵4老师 的回复:]使用#78楼的数据，在我#41楼代码的基础上优化了一下，运行不到一分钟： //二维数组中找最大特征数组 //http://bbs.csdn.net/topics/399169928 #pragma warning(disable:4996) //开头加这句或项目、属性、配置属性、C/C++、预处理器、预处理器定义中添加“_CRT_SECURE_NO_WARNINGS” #include <conio.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <search.h> #include <time.h>  #define N 50 //5000 static char a[N][N]={   {1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,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compare(const void *arg1,const void *arg2) {    return strncmp((char *)arg1,(char *)arg2,4*8); } bool isOldv(char *str) {     void *p;     if (V>0 && (p=bsearch((const void *)str,(const void *)&vstr[0],V,4*8,compare))!=NULL) {         CNTold++;         if (CNTold%100000==0) cprintf("y:%02d/%d CNTold:%d V:%d old pattern %d,%s\r",y,N,CNTold,V,((int)p-(int)&vstr[0][0])/(4*8),str);         return true;     } else {         if (V>=1024*1024*32) { //          printf("Warnning: Too many pattern!\n");             return false;         } else {             strncpy(vstr[V],str,4*8);             V++;             if (V%100000==0) cprintf("y:%02d/%d CNTold:%d V:%d new pattern:%s\r",y,N,CNTold,V,str);             return false;         }     } } unsigned long ulrand(void) {     return (      (((unsigned long)rand()<<24)&0xFF000000ul)     |(((unsigned long)rand()<<12)&0x00FFF000ul)     |(((unsigned long)rand()    )&0x00000FFFul)); }  int first1(int i) {     int j;     for (j=0;j<N;j++) {         if (a[i][j]==1) break;     }     return j; }  void next1(int i,int &p) {     int j;      if (p>=N) {p=N; return;}     for (j=p+1;j<N;j++) {         if (a[i][j]==1) break;     }     p=j; }  void test() {     int fitnum=0,b;     for (int i=0;i<N;i++) {         for (int j=first1(i);j<N-v[7];next1(i,j)) {             b=a[i][v[0]+j]+a[i][v[1]+j]+a[i][v[2]+j]+a[i][v[3]+j]+a[i][v[4]+j]+a[i][v[5]+j]+a[i][v[6]+j]+a[i][v[7]+j];             if (b==8) fitnum++;         }     }     if (fitnum>maxfitnum) {         bestv[0]=v[0];bestv[1]=v[1];bestv[2]=v[2];bestv[3]=v[3];bestv[4]=v[4];bestv[5]=v[5];bestv[6]=v[6];bestv[7]=v[7];         maxfitnum=fitnum;         printf("\nfound a new bestv,maxfitnum:%d\n",maxfitnum); //  } else { //      printf("fitnum:%d\n",fitnum);     } }  void recursion(int m,int L) {     if (L==8) { /*         printf("y:%04d u[0..7]:",y);         for (int i=0;i<8;i++) {             printf("%5d",u[i]);         } //      printf("\n"); */          for (int i=1;i<8;i++) v[i]=u[i]-u[0];         v[0]=0; /*         printf("|y:%04d v[0..7]:",y);         for (int i=0;i<8;i++) {             printf("%5d",v[i]);         }         printf("\n"); */         char ln[4*8+1];         for (int i=0;i<8;i++) sprintf(ln+i*4,"%04d",v[i]);         if (!isOldv(ln)) test();         return;     }     for (int i=m;i<=W-8+L;i++) {         u[L]=w[i];         recursion(i+1,L+1);     } }  void findpattern(int y) {     W=0;     for (int j=0;j<N;j++) {         if (a[y][j]==1) {             w[W]=j;             W++;         }     }     if (W>=8) { /*      printf("y:%04d W:%5d:",y,W);         for (int j=0;j<W;j++) {             printf("%5d",w[j]);         }         printf("\n"); */      recursion(0,0);     } }  int main() { //  srand(time(NULL));     srand(0); /*     for (int i=0;i<N;i++) {         for (int j=0;j<N;j++) {             if ((ulrand()%N) < 3) a[i][j]=1; //          printf("%d",a[i][j]);         } //      printf("\n");     } */     for (y=0;y<N;y++) {         findpattern(y);     }      if (maxfitnum==0) {         printf("\nCan not find pattern!");     } else {         printf("\nbestv:");         for (int i=0;i<8;i++) {             printf("%6d ",bestv[i]);         }     }     return 0; } // //found a new bestv,maxfitnum:450 //y:48/50 CNTold:26600000 V:346104 old pattern 182573,00000006000800140016001800340036 //bestv:     0      2      4      6      8     10     12     14   [/quote]   b[8]中的值不可重复

user: user-3
content: 可以考虑把每行看作一个二进制数字，对按行排序过的矩阵再做处理？这样可能不需要C(5000, 8)次循环了，只需要循环5000次，每次处理5000、4999... 1行数据

user: user-2
content: 引用 86 楼 赵4老师 的回复:使用#78楼的数据，在我#41楼代码的基础上优化了一下，运行不到一分钟： //二维数组中找最大特征数组 //http://bbs.csdn.net/topics/399169928 #pragma warning(disable:4996) //开头加这句或项目、属性、配置属性、C/C++、预处理器、预处理器定义中添加“_CRT_SECURE_NO_WARNINGS” #include <conio.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <search.h> #include <time.h>  #define N 50 //5000 static char a[N][N]={   {1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0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int compare(const void *arg1,const void *arg2) {    return strncmp((char *)arg1,(char *)arg2,4*8); } bool isOldv(char *str) {     void *p;     if (V>0 && (p=bsearch((const void *)str,(const void *)&vstr[0],V,4*8,compare))!=NULL) {         CNTold++;         if (CNTold%100000==0) cprintf("y:%02d/%d CNTold:%d V:%d old pattern %d,%s\r",y,N,CNTold,V,((int)p-(int)&vstr[0][0])/(4*8),str);         return true;     } else {         if (V>=1024*1024*32) { //          printf("Warnning: Too many pattern!\n");             return false;         } else {             strncpy(vstr[V],str,4*8);             V++;             if (V%100000==0) cprintf("y:%02d/%d CNTold:%d V:%d new pattern:%s\r",y,N,CNTold,V,str);             return false;         }     } } unsigned long ulrand(void) {     return (      (((unsigned long)rand()<<24)&0xFF000000ul)     |(((unsigned long)rand()<<12)&0x00FFF000ul)     |(((unsigned long)rand()    )&0x00000FFFul)); }  int first1(int i) {     int j;     for (j=0;j<N;j++) {         if (a[i][j]==1) break;     }     return j; }  void next1(int i,int &p) {     int j;      if (p>=N) {p=N; return;}     for (j=p+1;j<N;j++) {         if (a[i][j]==1) break;     }     p=j; }  void test() {     int fitnum=0,b;     for (int i=0;i<N;i++) {         for (int j=first1(i);j<N-v[7];next1(i,j)) {             b=a[i][v[0]+j]+a[i][v[1]+j]+a[i][v[2]+j]+a[i][v[3]+j]+a[i][v[4]+j]+a[i][v[5]+j]+a[i][v[6]+j]+a[i][v[7]+j];             if (b==8) fitnum++;         }     }     if (fitnum>maxfitnum) {         bestv[0]=v[0];bestv[1]=v[1];bestv[2]=v[2];bestv[3]=v[3];bestv[4]=v[4];bestv[5]=v[5];bestv[6]=v[6];bestv[7]=v[7];         maxfitnum=fitnum;         printf("\nfound a new bestv,maxfitnum:%d\n",maxfitnum); //  } else { //      printf("fitnum:%d\n",fitnum);     } }  void recursion(int m,int L) {     if (L==8) { /*         printf("y:%04d u[0..7]:",y);         for (int i=0;i<8;i++) {             printf("%5d",u[i]);         } //      printf("\n"); */          for (int i=1;i<8;i++) v[i]=u[i]-u[0];         v[0]=0; /*         printf("|y:%04d v[0..7]:",y);         for (int i=0;i<8;i++) {             printf("%5d",v[i]);         }         printf("\n"); */         char ln[4*8+1];         for (int i=0;i<8;i++) sprintf(ln+i*4,"%04d",v[i]);         if (!isOldv(ln)) test();         return;     }     for (int i=m;i<=W-8+L;i++) {         u[L]=w[i];         recursion(i+1,L+1);     } }  void findpattern(int y) {     W=0;     for (int j=0;j<N;j++) {         if (a[y][j]==1) {             w[W]=j;             W++;         }     }     if (W>=8) { /*      printf("y:%04d W:%5d:",y,W);         for (int j=0;j<W;j++) {             printf("%5d",w[j]);         }         printf("\n"); */      recursion(0,0);     } }  int main() { //  srand(time(NULL));     srand(0); /*     for (int i=0;i<N;i++) {         for (int j=0;j<N;j++) {             if ((ulrand()%N) < 3) a[i][j]=1; //          printf("%d",a[i][j]);         } //      printf("\n");     } */     for (y=0;y<N;y++) {         findpattern(y);     }      if (maxfitnum==0) {         printf("\nCan not find pattern!");     } else {         printf("\nbestv:");         for (int i=0;i<8;i++) {             printf("%6d ",bestv[i]);         }     }     return 0; } // //found a new bestv,maxfitnum:450 //y:48/50 CNTold:26600000 V:346104 old pattern 182573,00000006000800140016001800340036 //bestv:     0      2      4      6      8     10     12     14

user: user-4
content: C和C++完整教程:https://blog.csdn.net/it_xiangqiang/category_10581430.html C和C++算法完整教程:https://blog.csdn.net/it_xiangqiang/category_10768339.html

user: user-5
content: 使用#78楼的数据，在我#41楼代码的基础上优化了一下，运行不到一分钟： //二维数组中找最大特征数组 //http://bbs.csdn.net/topics/399169928 #pragma warning(disable:4996) //开头加这句或项目、属性、配置属性、C/C++、预处理器、预处理器定义中添加“_CRT_SECURE_NO_WARNINGS” #include <conio.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <search.h> #include <time.h>  #define N 50 //5000 static char a[N][N]={   {1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1},   {1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0},   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1},   {1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0},   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1},   {1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0},   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1},   {1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0},   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1}, }; int v[8],u[8],w[N],W,bestv[8],y; int maxfitnum=0; char vstr[1024*1024*32][4*8];//1GB int V=0,CNTold=0; int compare(const void *arg1,const void *arg2) {    return strncmp((char *)arg1,(char *)arg2,4*8); } bool isOldv(char *str) {     void *p;     if (V>0 && (p=bsearch((const void *)str,(const void *)&vstr[0],V,4*8,compare))!=NULL) {         CNTold++;         if (CNTold%100000==0) cprintf("y:%02d/%d CNTold:%d V:%d old pattern %d,%s\r",y,N,CNTold,V,((int)p-(int)&vstr[0][0])/(4*8),str);         return true;     } else {         if (V>=1024*1024*32) { //          printf("Warnning: Too many pattern!\n");             return false;         } else {             strncpy(vstr[V],str,4*8);             V++;             if (V%100000==0) cprintf("y:%02d/%d CNTold:%d V:%d new pattern:%s\r",y,N,CNTold,V,str);             return false;         }     } } unsigned long ulrand(void) {     return (      (((unsigned long)rand()<<24)&0xFF000000ul)     |(((unsigned long)rand()<<12)&0x00FFF000ul)     |(((unsigned long)rand()    )&0x00000FFFul)); }  int first1(int i) {     int j;     for (j=0;j<N;j++) {         if (a[i][j]==1) break;     }     return j; }  void next1(int i,int &p) {     int j;      if (p>=N) {p=N; return;}     for (j=p+1;j<N;j++) {         if (a[i][j]==1) break;     }     p=j; }  void test() {     int fitnum=0,b;     for (int i=0;i<N;i++) {         for (int j=first1(i);j<N-v[7];next1(i,j)) {             b=a[i][v[0]+j]+a[i][v[1]+j]+a[i][v[2]+j]+a[i][v[3]+j]+a[i][v[4]+j]+a[i][v[5]+j]+a[i][v[6]+j]+a[i][v[7]+j];             if (b==8) fitnum++;         }     }     if (fitnum>maxfitnum) {         bestv[0]=v[0];bestv[1]=v[1];bestv[2]=v[2];bestv[3]=v[3];bestv[4]=v[4];bestv[5]=v[5];bestv[6]=v[6];bestv[7]=v[7];         maxfitnum=fitnum;         printf("\nfound a new bestv,maxfitnum:%d\n",maxfitnum); //  } else { //      printf("fitnum:%d\n",fitnum);     } }  void recursion(int m,int L) {     if (L==8) { /*         printf("y:%04d u[0..7]:",y);         for (int i=0;i<8;i++) {             printf("%5d",u[i]);         } //      printf("\n"); */          for (int i=1;i<8;i++) v[i]=u[i]-u[0];         v[0]=0; /*         printf("|y:%04d v[0..7]:",y);         for (int i=0;i<8;i++) {             printf("%5d",v[i]);         }         printf("\n"); */         char ln[4*8+1];         for (int i=0;i<8;i++) sprintf(ln+i*4,"%04d",v[i]);         if (!isOldv(ln)) test();         return;     }     for (int i=m;i<=W-8+L;i++) {         u[L]=w[i];         recursion(i+1,L+1);     } }  void findpattern(int y) {     W=0;     for (int j=0;j<N;j++) {         if (a[y][j]==1) {             w[W]=j;             W++;         }     }     if (W>=8) { /*      printf("y:%04d W:%5d:",y,W);         for (int j=0;j<W;j++) {             printf("%5d",w[j]);         }         printf("\n"); */      recursion(0,0);     } }  int main() { //  srand(time(NULL));     srand(0); /*     for (int i=0;i<N;i++) {         for (int j=0;j<N;j++) {             if ((ulrand()%N) < 3) a[i][j]=1; //          printf("%d",a[i][j]);         } //      printf("\n");     } */     for (y=0;y<N;y++) {         findpattern(y);     }      if (maxfitnum==0) {         printf("\nCan not find pattern!");     } else {         printf("\nbestv:");         for (int i=0;i<8;i++) {             printf("%6d ",bestv[i]);         }     }     return 0; } // //found a new bestv,maxfitnum:450 //y:48/50 CNTold:26600000 V:346104 old pattern 182573,00000006000800140016001800340036 //bestv:     0      2      4      6      8     10     12     14

user: user-0
content: 是离散的8个1

user: user-6
content: 引用 82 楼 user-7的回复:外层循环5000次，内层每次记录下最后一个不符合要求的位置，以及当前对比到的最后位置，移动后只需要对比新增的位点，单纬度的特征提取。这样应该效率很高。你的题目好像是找连续的8个1啊 似乎可以是离散的八个1。实际上是任意抽8列，形成5000字节，里面的ff最多

user: user-0
content: 可以付费开发

user: user-0
content: 期待优化的算法，能在30天内算完就OK！

user: user-5
content: 引用 87 楼 早打大打打核战争 的回复:老赵的新代码确实不会死循环了 但是性能不行啊，在我的电脑上运行时间32秒。50x50数组和5000x5000数组的时间复杂度可不是1:10000的关系，大约是1:10^17的关系，所以你的代码要计算5000x5000数组在PC上需要大概1000亿年~  这就是穷举法的“绝对优势”！

user: user-3
content: 老赵的新代码确实不会死循环了 但是性能不行啊，在我的电脑上运行时间32秒。50x50数组和5000x5000数组的时间复杂度可不是1:10000的关系，大约是1:10^17的关系，所以你的代码要计算5000x5000数组在PC上需要大概1000亿年~

user: user-5
content: 审题！ 人家找的是同一行内8个点的分布特征，不是你画的二维特征。

user: user-7
content: 我想了个好方法，你把你的a[5000][5000]，画一个5000*5000方格的图片，1用红色填充，0用绿色填充，然后把图片导出来，用人眼看，找出可能的特征，然后再用程序验证。

user: user-7
content: 一张更好的图片，图片很小，你的a[5000][5000]那么大，查找量太大了。 如图：

user: user-7
content: 企图通过遍历查找特征是不可能的啊。 关键点在于你的b[8]是不确定，初期最好只检查b[8]的关键几个点的值。，然后你就可以进行移动扫描了。 让我们来举一个具体的例子，找出下面图中的最多的特征：    那么按照你的遍历算法，能找出来吗？