Fork me on GitHub


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;
}

 

posted on 2013-01-20 19:09  huashiyiqike  阅读(176)  评论(0编辑  收藏  举报