题目1149:子串计算 备份

本文介绍了一种算法,用于解决给定01字符串中每个子串出现次数的问题。输入为多行01字符串,输出按字典序排列的所有出现次数超过一次的子串及其出现次数。

http://ac.jobdu.com/problem.php?pid=1149
题目描述:

给出一个01字符串(长度不超过100),求其每一个子串出现的次数。

输入:

输入包含多行,每行一个字符串。

输出:

对每个字符串,输出它所有出现次数在1次以上的子串和这个子串出现的次数,输出按字典序排序。

样例输入:
10101
样例输出:
0 2
01 2
1 3
10 2
101 2

#include <stdio.h> #include <string.h> #include <stdlib.h> char arr[10000][101]; int cmp(const void * a,const void *b){ return strcmp((char*) a,(char *)b); } int main(){ char str[101]= {'\0'}; while(scanf("%s",str) != EOF){ int n = strlen(str); int index = 0; memset(arr,'\0',sizeof(arr)); for(int i=0; i<n; i++){ for(int j=i; j<n; j++){ int m = 0; char tmp[101]= {'\0'}; for(int k=i; k<=j; k++){ tmp[m++] = str[k]; } strcpy(arr[index++],tmp); } } qsort(arr,index+1,sizeof(char[101]),cmp); for( i=0; i<index; i++){ int count = 1; while(strcmp(arr[i],arr[i+1]) == 0) count++,i++; if(count>1) printf("%s %d\n",arr[i],count); } } return 0; }



//AC
#include<iostream>
#include<set>
#include<string>
using namespace std;
int main()
{
        string ss;
        int i,j,n,count;
        while(cin >> ss)
        {
                set<string> tt;
                n = ss.size();
                for(i = 0;i < n;i++)
                        for(j = 1;j <= n - i;j++)
                                tt.insert(ss.substr(i,j));
                set<string>::iterator it = tt.begin();
                for(;it != tt.end();it++)
                {
                        string tmp = *it;
                        n = ss.find(tmp);
                        count = 0;
                        while(n != ss.npos)
                        {
                                count++;
                                n = ss.find(tmp,n+1);
                        }
                        if(count > 1) cout << tmp << ' ' << count << endl;
                }
        }
        return 0;
}

 

转载于:https://www.cnblogs.com/huashiyiqike/articles/2868789.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值