编程珠玑 15.2 短语

查找字符串中最长重复字符串,方法:

后缀数组 + 快排

代码如下:

/*
 * pp_15_2.cpp
 *
 *  Created on: 2012-5-31
 *      Author: ict
 */
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
using namespace std;

#define MAXN 10000

//qsort比较函数
//这个函数注意指针的转换,需要理解???
int pstrcmp(const void *a, const void *b)
{
	return strcmp((char *)*(int *)a, (char *)*(int *)b);	//注意一定是(char *)*(int *)a,而不是(char *)*(char *)a
}

//比较两个字符串之间相同长度
int comlen(char *a, char *b)
{
	int i = 0;
	while(*a && *b && (*a++ == *b++))
		i++;

	return i;
}

int main()
{
	char c[MAXN];
	char *a[MAXN];
	char ch;

	int n;

	n = 0;
	//setbuf(stdout, NULL);
	FILE *fp;
	fp = fopen("c:\\data.txt", "r");

	while(fscanf(fp, "%c", &ch) != EOF)
	{
		a[n] = &c[n];
		c[n++] = ch;
	}
	c[n] = '\0';

	//排序后缀数组
	qsort(a, n, sizeof(char *), pstrcmp);

	int maxlen = 0;
	int maxi;

	for(int i= 0; i < n - 1; i++)
	{
		if(comlen(a[i], a[i+1]) > maxlen)
		{
			maxlen = comlen(a[i], a[i+1]);
			maxi = i;
		}

	}

	printf("%.*s\n", maxlen, a[maxi]);
	return 0;
}





你可能感兴趣的:(编程,c,File,null,FP)