nyoj1112 求次数 (对结构体字符串排序)

求次数

时间限制:1000 ms  |  内存限制:65535 KB

难度:2

描述

题意很简单,给一个数n 以及一个字符串str,区间【i,i+n-1】 为一个新的字符串,i 属于【0,strlen(str)】如果新的字符串出现过ans++,例如:acmacm n=3,那么 子串为acm cma mac acm ,只有acm出现过

求ans;

输入
LINE 1: T组数据(T<10)

LINE 2: n ,n <= 10,且小于strlen(str);

LINE 3:str

str 仅包含英文小写字母 ,切长度小于10w

输出
求 ans
样例输入
2
2
aaaaaaa
3
acmacm
样例输出
5
1
上传者

ACM_王亚龙

第一次看到10W以为会超时呢  然而并没有....

就是分割字符串,然后把分割的字符串存贮到结构体中(有点丢人了。只会用结构体的字符串排序,qsort的忘记了...)

然后对结构体的字符串进行排序,如果有两个字符串相同ans++;

代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define num 100005
char str[num],temp[11];
struct node
{
	char dir[11];
}c[num];
bool cmp(node x,node y)//对结构体的字符串排序
{
	if(strcmp(x.dir,y.dir)<0) return true;
	return false;
}
int main()
{
	int test,n,i,t,len;
	scanf("%d",&test);
	while(test--)
	{
		scanf("%d %s",&n,str);
		len=strlen(str);
		i=0;
		while(i+n<=len)//分割字符串并存贮字符串
		{
			t=0;
			for(int q=i;q<i+n;q++)
			temp[t++]=str[q];
			temp[t]='\0';
			strcpy(c[i].dir,temp);
			memset(temp,0,sizeof(temp));
			i++;
		}
		sort(c,c+i,cmp);
		int ans=0;
		for(int j=1;j<i;j++)
		if(strcmp(c[j].dir,c[j-1].dir)==0)
		ans++;
		printf("%d\n",ans);
	}
	return 0;
}
时间: 2024-08-17 12:12:13

nyoj1112 求次数 (对结构体字符串排序)的相关文章

C++ list结构体变量排序

以下内容是自己整理的根据结构体里面的不同变量,对list排序的实例,若有问题可以留言.仅供参考. #include <iostream>#include <list>#include <algorithm> using namespace std; //声明结构体typedef struct testListSort{ int number; std::string name; char time[10]; int datalen; }stuTest; //结构体lis

C++线性表通过结构体实现操作和结构体字符串快速排序和shell排序结合

#include<iostream> #include<string> #define ml 10 using namespace std; typedef struct{//定义Data数据项 std::string name; long num; }Data; struct Link{//定义结构体 Data data[ml+1]; int length; }L; void initLink(Link *p){//初始化,即便有数据可以覆盖写入增加效率 p->length

C++中的结构体vector排序

在包含了头文件#include <algorithm>之后,就可以直接利用sort函数对一个vector进行排序了: 1 // sort algorithm example 2 #include <iostream> // std::cout 3 #include <algorithm> // std::sort 4 #include <vector> // std::vector 5 6 bool myfunction (int i,int j) { re

结构体指针排序

输入不超过30名学生的信息,包括姓名,单科分数,出生年月,对其排序后输出. 运用知识点:结构体.指针.排序.函数. 1 #include<stdio.h> 2 #include<string.h> 3 4 struct birth 5 { 6 int y; 7 int m; 8 int d; 9 }; 10 struct student 11 { 12 int num; 13 char name[20]; 14 double score; 15 struct birth birth

C语言 &#183; 运用结构体的排序方法

之前遇到排序只想着最原始的方法,诸如冒泡,选择,快速排序等等,刚刚跟大牛学会了结构体的方法来排序,这样的话以后再也不用怕成绩统计.名次排序之类的题目了. 首先头文件(基于大牛的方法,本人之后做题喜欢引入题目中常用的五个头文件) #include<stdlib.h> #include<string.h> 定义结构体: /*定义一个结构体*/ typedef struct Stu{ char name[10]; int id; int score; }stu; 注释:最后一行stu是别

Go结构体切片排序

package main import ( "fmt" "sort" "math/rand" ) //1.声明Hero结构体 type Hero struct{ Name string Age int } //2.声明一个Hero结构体切片类型 type HeroSlice []Hero //3.实现Interface接口(名字就叫interface) //有三个方法:Len() Less() Swap(),实现这个方法就实现了接口 func (

【C项目】 文件,结构体,链表,排序, 学生信息管理系统

1.密码登录: 2.通过键盘输入学生信息,包括学生学号.姓名.成绩: 3.将输入的信息保存到指定文件中: 4.从文件中读取学生信息: 5.显示全部学生信息: 6.按学生总分进行降序排序: 7.删除学生信息: 8.查找学生信息,可以通过学号查找,也可以通过姓名查找: 9.统计学生信息,统计出最高分和学生人数: 10.退出系统: 源代码: [email protected]:~$ cat main.c  #include<stdio.h> #include<stdlib.h> #inc

Codeforces 801C Voltage Keepsake 结构体sort排序+贪心

#include <bits/stdc++.h> using namespace std; struct In{ long long a, b; double t; }s[100005]; bool cmp(In p, In q) { if(p.t < q.t) return true; else if(p.t == q.t && p.a < q.a) return true; return false; } int main() { long long n, p;

go语言的排序、结构体排序

原文:https://studygolang.com/articles/1598 晚上准备动手写点 go 的程序的时候,想起 go 如何排序的问题.排序 sort 是个基本的操作,当然搜索 search 也是.c 提供一个 qsort 和 bsearch,一个快排一个二分查找,不过是使用起来都不方便: c++ 中的 sort 貌似很不错,因为 c++ 支持泛型(或是说模板),所以很多东西使用起来很方便.go 是通过 sort 包提供排序和搜索,因为 go 暂时不支持泛型(将来也不好说支不支持),