栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

【算法】kstring(字符串子序列重排序)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【算法】kstring(字符串子序列重排序)

给定一个长度为n
的只包含小写字母的字符串S,请对所有长度为k的子串进行排序,输出排好序的下标序列。

按照字典序从小到大排序,如果子串一样,那么左端点下标小的更小(排在前面)。

1、结构体,sort比较函数重定义

Memory Limit Execeed(爆内存,5分)

#include 
#include 
#include 
using namespace std;
struct Node{
	string a;
	int index;
};
bool cmp(Node x,Node y){
	return x.a < y.a;
}
int main()
{
	int k;
	string input;
	cin>>input>>k;
	int len = input.size()-k+1;
	struct Node node[len];
	for(int i=0;i 
2、pair,sort排序重定义 

爆内存,5分

#include 
#include 
#include 
using namespace std;
bool cmp(pair  x,pair  y){
	return x.first < y.first;
}
int main()
{
	int k;
	string input;
	cin>>input>>k;
	int len = input.size()-k+1;
	pair  node[len];
	for(int i=0;i 
3、multimap,排序都省了 

TLE,cout7分,printf8分

#include 
using namespace std;
int main()
{
	int k;
	string input;
	cin>>input>>k;
	int len = input.size()-k+1;
	for(int a=0; a<26; a++) {
		multimap node;
		for(int i=0; i(input.substr(i,k),i+1));
		}
		if(!node.size()) continue;
		multimap ::iterator it ;
		for(it= node.begin(); it!=node.end(); ++it) {
			printf("%d ",it->second);
		}
		node.clear();
	}
	return 0;
}
4、节约空间的思想

勉强10分

#include 
#include 
#include 
#include 
using namespace std;
typedef struct Node {
	string a;
	int index,tail;
} N;
bool cmp(N x,N y)
{
	return x.a < y.a;
}
int main()
{
	int k;
	string input,pre="";
	cin>>input>>k;
	int len = input.size()-k+1;
	vector node;
	for(int a=0; a<26; a++) {
		for(int i=1; i<=len; i++) {
			char head = input[i-1];
			if(head == (char)(a+'a')) {
				string subs = input.substr(i-1,k);
				if(pre != subs) {
					pre = subs;
					N tmp;
					tmp.a  =  subs;
					tmp.index = i;
					tmp.tail = i;
					node.push_back(tmp);
				} else {
					node[node.size()-1].tail = i;
				}
			}

		}
		if(!node.size()) continue;
		sort(node.begin(),node.end(),cmp);
		for(int i=0; i
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/317612.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号