标签:style blog color os io 2014
#include<iostream> #include<string.h> #include<map> #include<algorithm> using namespace std; struct scmp { bool operator()(const char *s1, const char *s2) const { return strcmp(s1,s2)<0; } }; int mycmp(const void *p1, const void *p2) { return strcmp(*(char **)p1, *(char **)p2); } int comlen(char *p,char *q) { int len=0; while(*p&&*q&&*p++==*q++) len++; return len; } int main() { char s[50]; char *a[51]; int cnt[50]={0}; map<char*,int,scmp> chmap; int i,n,max=0; cin>>s; n=strlen(s); if(n==0) return 0; for(i=0;i<n;i++) a[i]=&s[i]; a[n]=0; qsort(a,n,sizeof(char *),mycmp); for(i=0;i<n-1;i++) { int temp=comlen(a[i],a[i+1]); if(max<temp) { max=temp; } cnt[i]=temp; } for(i=0;i<n;i++) if(cnt[i]==max) { char *subs=new char[50]; strncpy(subs,a[i],max); subs[max]='\0'; map<char*,int,scmp>::iterator it=chmap.find(subs); if(it!=chmap.end()) { it->second++; } else chmap.insert(make_pair(subs,2)); } cout<<"Result:"<<endl; for(map<char*,int,scmp>::iterator i=chmap.begin();i!=chmap.end();i++) cout<<i->first<<" "<<i->second<<endl; for(map<char*,int,scmp>::iterator i=chmap.begin();i!=chmap.end();i++) delete i->first; return 0; }
找出所有最长连续重复子串及其个数,布布扣,bubuko.com
标签:style blog color os io 2014
原文地址:http://blog.csdn.net/longhopefor/article/details/38069923