一个一万字长的连续字符串,找出其所有子串,并统计子串的出现次数

ShadowWonder 2013-05-06 04:51:16
如题,最好使用C/C++代码实现,如果有仁兄用过类似软件,请介绍给我本人使用,大恩大德,永世难忘。
...全文
243 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
ShadowWonder 2013-05-07
  • 打赏
  • 举报
回复
引用 8 楼 usingnamespace_std 的回复:
[quote=引用 6 楼 ShadowWonder 的回复:] [quote=引用 4 楼 usingnamespace_std 的回复:] 我很暴力的, lz可以试试后缀树,以下代码暴力实现,算法复杂度,m^3/2log(m^2/2) m是单词长度

#include <iostream>
#include <cstdio>
#include <map>
#include <string>
#include <time.h>
using namespace std;

map<string, int> dict;

int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
	string s;
	double t = clock();
	while(cin >> s)
	{
		dict.clear();
		for (int i=0; i<s.length(); i++)
		{
			for (int j=1; j+i-1<s.length(); j++)
			{
				dict[s.substr(i, j)] ++;
			}
		}

		for(map<string, int>::iterator it=dict.begin(); it!=dict.end(); it++)
		{
			cout << it->first << " : " << it->second << endl;
		}

		cout << "字符串长度10009, 运行时间为:"<< clock()-t << " ms." << endl;
	}
    
	
	return 0;
}

请问高手用的编译器是?.... 另外,我要统计每个子串出现的次数喔。[/quote] Visual Stadio 2010 次数有统计啊[/quote] 后缀树的资料哪里有。。。
Jackie_Zhu 2013-05-07
  • 打赏
  • 举报
回复
引用 6 楼 ShadowWonder 的回复:
[quote=引用 4 楼 usingnamespace_std 的回复:] 我很暴力的, lz可以试试后缀树,以下代码暴力实现,算法复杂度,m^3/2log(m^2/2) m是单词长度

#include <iostream>
#include <cstdio>
#include <map>
#include <string>
#include <time.h>
using namespace std;

map<string, int> dict;

int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
	string s;
	double t = clock();
	while(cin >> s)
	{
		dict.clear();
		for (int i=0; i<s.length(); i++)
		{
			for (int j=1; j+i-1<s.length(); j++)
			{
				dict[s.substr(i, j)] ++;
			}
		}

		for(map<string, int>::iterator it=dict.begin(); it!=dict.end(); it++)
		{
			cout << it->first << " : " << it->second << endl;
		}

		cout << "字符串长度10009, 运行时间为:"<< clock()-t << " ms." << endl;
	}
    
	
	return 0;
}

请问高手用的编译器是?.... 另外,我要统计每个子串出现的次数喔。[/quote] Visual Stadio 2010 次数有统计啊
ShadowWonder 2013-05-07
  • 打赏
  • 举报
回复
引用 5 楼 neofung 的回复:
详情可以参考罗穗骞关于后缀数组的论文,这里给出代码:

#include "stdafx.h"
/*******************************************************************************
# Author : Neo Fung
# Email : neosfung@gmail.com
# Last modified: 2012-06-03 09:45
# Filename: SPOJ694 Distinct Substrings.cpp
# Description : 输入array的最后一位必须为0,且其他元素大于0
******************************************************************************/
#ifdef _MSC_VER
#define _CRT_SECURE_NO_DEPRECATE
#endif

#include <fstream>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <string>
#include <limits.h>
#include <algorithm>
#include <math.h>
#include <numeric>
#include <functional>
#include <ctype.h>
using namespace std;

const int kMAXN=20010;

int w[kMAXN],wa[kMAXN],wb[kMAXN],wv[kMAXN],array[kMAXN],sa[kMAXN];
char str[kMAXN];

// 
int cmp(const int *r,const int &a,const int &b,const int &l){
	return r[a]==r[b]&&r[a+l]==r[b+l];
}

// 倍增算法
// r为待比较数组;n为r的长度;m为r中元素的最大值 
void DoublingAlgorithm(const int *r,int *sa,const int &n,int m){
	int i,j,p,*x=wa,*y=wb,*t;
	for (i=0;i<m;i++) w[i]=0;
	for (i=0;i<n;i++) w[x[i]=r[i]]++;
	for (i=1;i<m;i++) w[i]+=w[i-1];
	for (i=n-1;i>=0;i--) sa[--w[x[i]]]=i;
	for (p=1,j=1;p<n;j*=2,m=p){
		for (p=0,i=n-j;i<n;i++) y[p++]=i;
		for (i=0;i<n;i++) if (sa[i]>=j) y[p++]=sa[i]-j;
		for (i=0;i<m;i++) w[i]=0;
		for (i=0;i<n;i++) w[wv[i]=x[y[i]]]++;
		for (i=1;i<m;i++) w[i]+=w[i-1];
		for (i=n-1;i>=0;i--) sa[--w[wv[i]]]=y[i];
		for (t=x,x=y,y=t,p=1,i=1,x[sa[0]]=0;i<n;i++)
			x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
	}
	return;
}

// 计算height数组,r和sa的定义同上;
// height的下标从1开始,到n结束,包含n;
int rank[kMAXN],height[kMAXN];
void CalculateHeight(const int *r,int *sa,const int &n){
	int i,j,k=0;
	for (i=1;i<=n;i++) rank[sa[i]]=i;
	for (i=0;i<n;height[rank[i++]]=k)
		for (k?k--:0,j=sa[rank[i]-1];r[i+k]==r[j+k];k++);
	return;
}

int _tmain(int argc, _TCHAR* argv[])
{
#ifdef DEBUG  
	freopen("../stdin.txt","r",stdin);
	freopen("../stdout.txt","w",stdout); 
#endif  

	strcpy(str,"hello");

	int n=strlen(str);
	for(int i=0;i<n;++i)
		array[i]=str[i];
	array[n++]=0;
	DoublingAlgorithm(array,sa,n,128);
	CalculateHeight(array,sa,n-1);

	long long ans=0ll;
	for(int i=1;i<n;++i){
		int tmp=n-1-sa[i]-height[i];
		ans+=tmp;
		for (int j=n-1;j>sa[i]+height[i];--j)
		{
			for(int k=sa[i];k<j;++k)
				putchar(str[k]);
			puts("");
		}
	}

	printf("\ntotal: %lld\n",ans);

	return 0;
}
请问高手用的编译器是?.... 另外,我要统计每个子串出现的次数喔。
ShadowWonder 2013-05-07
  • 打赏
  • 举报
回复
引用 4 楼 usingnamespace_std 的回复:
我很暴力的, lz可以试试后缀树,以下代码暴力实现,算法复杂度,m^3/2log(m^2/2) m是单词长度

#include <iostream>
#include <cstdio>
#include <map>
#include <string>
#include <time.h>
using namespace std;

map<string, int> dict;

int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
	string s;
	double t = clock();
	while(cin >> s)
	{
		dict.clear();
		for (int i=0; i<s.length(); i++)
		{
			for (int j=1; j+i-1<s.length(); j++)
			{
				dict[s.substr(i, j)] ++;
			}
		}

		for(map<string, int>::iterator it=dict.begin(); it!=dict.end(); it++)
		{
			cout << it->first << " : " << it->second << endl;
		}

		cout << "字符串长度10009, 运行时间为:"<< clock()-t << " ms." << endl;
	}
    
	
	return 0;
}

请问高手用的编译器是?.... 另外,我要统计每个子串出现的次数喔。
neofung 2013-05-06
  • 打赏
  • 举报
回复
详情可以参考罗穗骞关于后缀数组的论文,这里给出代码:

#include "stdafx.h"
/*******************************************************************************
# Author : Neo Fung
# Email : neosfung@gmail.com
# Last modified: 2012-06-03 09:45
# Filename: SPOJ694 Distinct Substrings.cpp
# Description : 输入array的最后一位必须为0,且其他元素大于0
******************************************************************************/
#ifdef _MSC_VER
#define _CRT_SECURE_NO_DEPRECATE
#endif

#include <fstream>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <string>
#include <limits.h>
#include <algorithm>
#include <math.h>
#include <numeric>
#include <functional>
#include <ctype.h>
using namespace std;

const int kMAXN=20010;

int w[kMAXN],wa[kMAXN],wb[kMAXN],wv[kMAXN],array[kMAXN],sa[kMAXN];
char str[kMAXN];

// 
int cmp(const int *r,const int &a,const int &b,const int &l){
	return r[a]==r[b]&&r[a+l]==r[b+l];
}

// 倍增算法
// r为待比较数组;n为r的长度;m为r中元素的最大值 
void DoublingAlgorithm(const int *r,int *sa,const int &n,int m){
	int i,j,p,*x=wa,*y=wb,*t;
	for (i=0;i<m;i++) w[i]=0;
	for (i=0;i<n;i++) w[x[i]=r[i]]++;
	for (i=1;i<m;i++) w[i]+=w[i-1];
	for (i=n-1;i>=0;i--) sa[--w[x[i]]]=i;
	for (p=1,j=1;p<n;j*=2,m=p){
		for (p=0,i=n-j;i<n;i++) y[p++]=i;
		for (i=0;i<n;i++) if (sa[i]>=j) y[p++]=sa[i]-j;
		for (i=0;i<m;i++) w[i]=0;
		for (i=0;i<n;i++) w[wv[i]=x[y[i]]]++;
		for (i=1;i<m;i++) w[i]+=w[i-1];
		for (i=n-1;i>=0;i--) sa[--w[wv[i]]]=y[i];
		for (t=x,x=y,y=t,p=1,i=1,x[sa[0]]=0;i<n;i++)
			x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
	}
	return;
}

// 计算height数组,r和sa的定义同上;
// height的下标从1开始,到n结束,包含n;
int rank[kMAXN],height[kMAXN];
void CalculateHeight(const int *r,int *sa,const int &n){
	int i,j,k=0;
	for (i=1;i<=n;i++) rank[sa[i]]=i;
	for (i=0;i<n;height[rank[i++]]=k)
		for (k?k--:0,j=sa[rank[i]-1];r[i+k]==r[j+k];k++);
	return;
}

int _tmain(int argc, _TCHAR* argv[])
{
#ifdef DEBUG  
	freopen("../stdin.txt","r",stdin);
	freopen("../stdout.txt","w",stdout); 
#endif  

	strcpy(str,"hello");

	int n=strlen(str);
	for(int i=0;i<n;++i)
		array[i]=str[i];
	array[n++]=0;
	DoublingAlgorithm(array,sa,n,128);
	CalculateHeight(array,sa,n-1);

	long long ans=0ll;
	for(int i=1;i<n;++i){
		int tmp=n-1-sa[i]-height[i];
		ans+=tmp;
		for (int j=n-1;j>sa[i]+height[i];--j)
		{
			for(int k=sa[i];k<j;++k)
				putchar(str[k]);
			puts("");
		}
	}

	printf("\ntotal: %lld\n",ans);

	return 0;
}
Jackie_Zhu 2013-05-06
  • 打赏
  • 举报
回复
我很暴力的, lz可以试试后缀树,以下代码暴力实现,算法复杂度,m^3/2log(m^2/2) m是单词长度

#include <iostream>
#include <cstdio>
#include <map>
#include <string>
#include <time.h>
using namespace std;

map<string, int> dict;

int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
	string s;
	double t = clock();
	while(cin >> s)
	{
		dict.clear();
		for (int i=0; i<s.length(); i++)
		{
			for (int j=1; j+i-1<s.length(); j++)
			{
				dict[s.substr(i, j)] ++;
			}
		}

		for(map<string, int>::iterator it=dict.begin(); it!=dict.end(); it++)
		{
			cout << it->first << " : " << it->second << endl;
		}

		cout << "字符串长度10009, 运行时间为:"<< clock()-t << " ms." << endl;
	}
    
	
	return 0;
}

vssvss 2013-05-06
  • 打赏
  • 举报
回复
后缀树 试试看 效率不错的
nice_cxf 2013-05-06
  • 打赏
  • 举报
回复
...一个起点,一个终点,总共大概是5千万个字符串,还要统计。。。
撸大湿 2013-05-06
  • 打赏
  • 举报
回复
搜索一下“C++ 分词” 答案很多地~~
课程介绍:第一章:正则表达式(regularexpression)描述了一种字符串匹配的模式(pattern),可以用来检查一个串是否含有某种子串、将匹配的子串替换或者从某个串中取出符合某个条件的子串等。第二章:http协议是一种无状态协议,不记录用户行为,我们可以利用cookie记录数据,方便用户操作,提升用户体验。第三章:ECMAScript6.0(以下简称ES6)是JavaScript语言的下一代标准,已经在2015年6月正式发布了。它的目标,是使得JavaScript语言可以用来编写复杂的大型应用程序,成为企业级开发语言。第四章:本章主要讲解JS动画原理、动画函数封装和轮播。第五章:本章主要讲解面向对象、构造函数和继承、原型链和继承。第六章:本节课程主要讲解了什么是Ajax、如何使用Ajax发送get请求、如何使用Ajax发送post请求、JSON数据格式、回调地狱、Promise和Ajax的同源策略、跨域请求。第七章:本章主要讲解html、val、attr、prop、class、全选框、动画、节点遍历、ajax、sonp、event、multiple、plugin、plugin、magnifier。第八章:本章主要讲解UML类图、单例模式、工厂模式、策略模式、代理模式、观察者模式。第九章:本章主要讲解为什么要模块化、原生JS中,模块的写法、AMD、CommonJS&Webpack。第十章:本节课程主要讲解了服务器安装环境配置、端口及ip基本常识、简单认识PHP(helloworld)、基本语法和动态网页原理。第十一章:本节课程主要讲解了什么是SASS、SASS的预处理、ass语法(变量、嵌套、导入、mixin、扩展、function、expression)。第十二章:本节课程主要讲解了什么是GULP、GULP环境配置、GULP基本使用及GULP的插件安装与使用。

33,008

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧