String and multidimensional arrays

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/u013345179/article/details/102761371

Defined strings

A finite sequence of a string of zero or more characters, a string containing only blank spaces called strings. The number of characters included in the string length of the string is called, called zero-length string empty string. The first character string virtue consecutive characters called sub-sequences of the sub-string string string string number is referred to in the main sub-string in the main string position.

String comparison is based on each character lexicographically row has an ASCII code which represents the size of the relationship between the characters.

Pattern matching, a main character from the first string in the beginning and mode T (T is to match the sub-string) of the first character comparison is more follow-up until the match is completed if the same, otherwise the main character strings moved back a re-match.

int Index(string s, string t){
    int lens = s.length();//计算串s、t的长度
    int lent = t.length();
    int i = 0;
    int j = 0;
    while (i < lens&&j < lent){//如果i、j都各自小于lens和lent
        if (t[j] == s[i]){//如果子串的t[j]和主串的s[i]相等
            ++i;//各自索引都自增
            ++j;
        }
        else{//否则,主串的索引比刚开始后移一个;子串的索引变为0
            i = i - j + 1;
            j = 0;
        }
    }
    if (j == lent){//如果最j和lent的大小一样,证明找到了,返回子串在主串中的索引
        return i - lent;
    }
    else{//否则返回-1
        return -1;
    }
}

kmp algorithm a NEXT NEXT key is stored in the array array [i] i is represented by the string and ending a primary maximum matching string length of the first start.

next(char *T ,int next[])
{
	int i=1,j=0,k=0;
	int Tlen = strlen(T);
	next[0] = 0;
	next[1] = 0;
	while(i < Tlen)
	{
		printf("next while %d,T[i%d]=%c,T[j%d]=%c\n",k++,i,T[i],j,T[j]);
		if(T[i] == T[j] || j==0)
		{
			++i;
			++j;
			next[i] = j;
		} else {
			j = next[j];
		}
	}
	for(i=0 ; i<Tlen ; i++)
		printf(" next[%d]= %d ",i,next[i]);
	printf("\n");
	return;
}

 Storage arrays

 Is always one-dimensional array is stored in memory.

 Compressed storage

Symmetrical array of compressed storage need only keep the upper triangular or lower triangular matrix line

Diagonal matrix storage

eg。

2,1,0,0,0,
 9,3,4,0,0,
 0,7,5,3,0,
 0,0,5,4,2,
 0,0,0,1,1

Only data on both sides of the diagonal and two way is to store all lines t is pressed to the side so that the compressed matrix is ​​row s compression columns of the matrix, then t = i, s = j-i +1; another way is stored by row

Since too sparse matrix 0 can omit the blank record only a triple data and each coordinate data consisting of an array of structures can be achieved.

Or a method of using cross-linked list of data base node type zero node comprises a pointer to the next two nonzero peer node and the next column of the same.

Guess you like

Origin blog.csdn.net/u013345179/article/details/102761371