C++ 子串計算

#include "iostream"  
#include "stdio.h"  
#include "math.h"  
#include "map"  
#include "vector"  
#include "queue"  
#include "memory.h"  
#include "algorithm"  
#include "string"  
using namespace std;  
#define N 105  
#define INF 1<<30  
   
struct DIC
{
    char s[105];
}d[100000];
 
int comp(const void *a,const void *b)
{
    DIC *A=(DIC*)a;
    DIC* B=(DIC*)b;
    return strcmp(A->s,B->s);
}
   
int main()
{
    char str[105];
    int i,j,k,ind=0;
    char tmp[105];
    while(cin>>str)
    {
        ind=0;
        int len=strlen(str);
        for(i=1;i<=len;i++)
        {
            for(j=0;j+i<=len;j++)
            {                
                memset(tmp,'\0',sizeof(tmp));
                for(k=0;k<i;k++)
                    tmp[k]=str[k+j];
                strcpy(d[ind++].s,tmp);
            }
        }
        qsort(d,ind,sizeof(d[0]),comp);
        for(i=0;i<ind;i++)
        {
            int num=1;
            while(i+1<ind&&strcmp(d[i].s,d[i+1].s)==0) 
                num++,i++;
            if(num>1)
                printf("%s %d\n",d[i].s,num);
        }
    }
}

—————————————————————————————————

本文原創自Sliencecsdn技術博客。

本博客所有原創文章請以鏈接形式註明出處。

歡迎關注本技術博客,本博客的文章會不定期更新。


大多數人想要改造這個世界,但卻罕有人想改造自己。

世上沒有絕望的處境,只有對處境絕望的人。

                                              ————By slience

—————————————————————————————————


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章