Find the substring with the longest repeat times in a string and calculate its repeat times. For example, the string "ABC fghi bc kl abcd lkm abcdefg" and returns "ABCD" and 2.
Because the question requires that the longest substring be repeated at least twice, the focus is on the longest substring, not the maximum number of repetitions. Therefore, we can start with the longest string and calculate its repetition times. The string can be returned as long as it is repeated twice.
#include <stdio.h>#include <stdlib.h>#include <string.h>int find_longest_dup_str(char* src, char* dest);int str_sub(char* src, char* sub, int pos,int len);int str_cnt(char* src,char* sub);int main(int argc, char* argv[]){ char str[] = "abc fghi bc kl abcd lkm abcdefg"; char sub[128] = ""; int cnt; cnt = find_longest_dup_str(str,sub); printf("result: %s\ncnt: %d \n",sub,cnt);}int find_longest_dup_str(char* src, char* dest){ int len; int index; int cnt; for(len = strlen(src);len > 0;len--) { for(index = 0; index + len-1 < strlen(src); index++) { str_sub(src,dest,index,len); if((cnt = str_cnt(src,dest)) >= 2) { return cnt; } } } return 0;}int str_sub(char* src, char* sub, int pos,int len){ int index; for(index = 0; index < len;index++) { sub[index] = src[pos + index]; } sub[index] = ‘\0‘;}int str_cnt(char* src,char* sub){ int cnt = 0; char tmp[128]; int index; int index_sub; for(index = 0;index + strlen(sub)-1 < strlen(src);index++) { /* method1 for(index_sub = 0; index_sub < strlen(sub);index_sub++) { if(src[index + index_sub] != sub[index_sub]) { break; } } if(index_sub == strlen(sub)) { cnt++; } */ /* method2 str_sub(src,tmp,index,strlen(sub)); if(strcmp(sub,tmp) == 0) { cnt++; } */ // method3 if(strncmp(src + index,sub,strlen(sub))== 0) { cnt++; } } return cnt;}
The natural number is filled into the array in a snake-like arrangement. Replace natural numbers 1, 2, 3... And N * n Insert the proper positions in the square matrix sequentially. This process is performed along the oblique column. Numbers of oblique columns: 0, 1, 2... , 2n (marked with I, n = N-1), the following data arrangement, which is a snake-like arrangement.
1 3 4 10
2 5 9 11
6 8 12 15
7 13 14 16
(0, 0) and 0
() And 1
() And 2
() And 3
() And 4 -->)
() And 5 -->)
() And 6 -->)
It is not difficult to find that as long as the number is on the same oblique line, the sum of the subscript corresponding to the array row and column is always equal.
Note: When the sum is 4 or greater than 4, you need to pay attention to the out-of-boundary issue when filling the number, so I specifically wrote a macro in the program to judge.
#include <stdio.h>#include <stdlib.h>#include <string.h>#define N 4#define IS_LEGAL(row,col) ((row) >= 0 && (row) < N && (col) >= 0 && (col) < N )void print(int (*arr)[N]);void fill(int (*arr)[N]);int main(int argc, char *argv[]){ int arr[N][N]; memset(arr,0,sizeof(arr)); fill(arr); print(arr); return 0;}void print(int (*arr)[N]){ int i,j; for(i = 0; i < N; i++) { for(j = 0; j < N; j++) { printf("%3d",arr[i][j]); } printf("\n"); }}void fill(int (*arr)[N]){ int sum; int row,col; int num = 0; for(sum = 0; sum < 2 * N - 1; sum++) { if(sum % 2 == 0) { for(row = 0; row < N; row++) { col = sum - row; if(IS_LEGAL(row,col)) { arr[row][col] = ++num; } } }else { for(row = sum; row >=0; row--) { col = sum - row; if(IS_LEGAL(row,col)) { arr[row][col] = ++num; } } } }}
Print cube arrays. cube arrays refer to the principle that the sum of each row, column, and diagonal line is equal. For example, the third-level cube array:
8 1 6
3 5 7
4 9 2
Program and print the odd-order cube array.
Prompt
The key to solving the problem is to fill the element. The first element 1 is located in the middle of the first line, and the new position should be in the upper right corner of the recently inserted element; however, if the position in the upper-right corner of the Rule exceeds the upper boundary of the rule, the next position of the column should be taken for the new position. If the position in the upper-right corner exceeds the upper boundary of the rule, the leftmost position of the row will be taken; if the most recently inserted element is an integer multiple of N, the position in the same column in the following row is selected as the new position.
Implementation Code
# Include <stdio. h> # include <stdlib. h> # include <string. h> int main (INT argc, char * argv []) {int size; int row, Col; int num = 0; int index; size = atoi (argv [1]);/* dynamically allocates two-dimensional arrays for storing cube arrays */INT ** arr = (INT **) calloc (size, sizeof (int *); For (Index = 0; index <size; index ++) {arr [Index] = (int *) calloc (size, sizeof (INT);}/* store cube arrays */ROW = 0; Col = size> 1; arr [row] [col] = ++ num; while (Num <size * size) {If (Num % size = 0) {ROW = (row + 1) % size; Col = Col; arr [row] [col] = ++ num;} else {ROW = (Row-1 + size) % size; Col = (COL + 1) % size; arr [row] [col] = ++ num ;}/ * print */For (ROW = 0; row <size; row ++) {for (COL = 0; col <size; Col ++) {printf ("%-3D", arr [row] [col]);} printf ("\ n ");} /* release the bucket */For (Index = 0; index <size; index ++) {free (ARR [Index]); arr [Index] = NULL ;} free (ARR); arr = NULL; return 0 ;}
C algorithm question