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

来源:互联网 发布:java投票统计 编辑:程序博客网 时间:2024/05/29 15:31

求次数

时间限制: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
样例输入
22aaaaaaa3acmacm
样例输出
51
上传者

ACM_王亚龙

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

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

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

代码:

#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;#define num 100005char 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;}


1 0
原创粉丝点击