Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <cmath>
- using namespace std;
- int String2Int(char *str, int len)
- {
- int temp = 0;
- int s = 1;
- for (int i = len-1; i>=0 ;i--)
- {
- temp += (str[i]-'0')*s;
- s *= 2;
- }
- return temp;
- }
- char res[13];
- char* Int2String(int num,int len)
- {
- res[len] = '\0';
- for (int i = len-1 ; i>=0 ;i--)
- {
- if (num)
- {
- res[i] = '0' + num%2;
- num = num/2;
- }
- else
- {
- res[i] = '0';
- }
- }
- return res;
- }
- int hash[13][10000];//len,binary
- char input[200001];
- class item
- {
- public:
- int freq,len,num;
- };
- item topn[100000];//
- int cmp(const void* a, const void* b)
- {
- if (((item*)a)->freq ==((item*)b)->freq)
- {
- if (((item*)a)->len ==((item*)b)->len)
- {
- return ((item*)a)->num - ((item*)b)->num;
- }
- else
- {
- return ((item*)a)->len - ((item*)b)->len;
- }
- }
- else
- {
- return ((item*)b)->freq - ((item*)a)->freq;
- }
- }
- int main()
- {
- ifstream fin("contact.in");
- ofstream fout("contact.out");
- int A,B,N;
- fin>>A>>B>>N;
- char c;
- int size = 0;
- while ( (c = fin.get())!= EOF )
- {
- if (c=='\n');
- else
- input[size++] = c;
- }
- //process
- char buf[13];
- for ( int i = 0; i<size; i++ )
- {
- for (int length = A; length<=B ;length++)
- {
- if (i+length<=size)
- {
- for (int j = i ; j<i+length ;j++)
- {
- buf[j-i] = input[j];
- }
- hash[length][String2Int(buf,length)]++;
- }
- }
- }
- //sort
- int size_top = 0;
- for (int i = A ; i<=B ;i++)
- {
- for (int j =0 ; j<pow(2.0,i+1) ; j++)//attention here is i+1 instead of i.
- {
- topn[size_top].freq = hash[i][j];
- topn[size_top].len = i;
- topn[size_top].num = j;
- size_top++;
- }
- }
- qsort(topn,size_top,sizeof(item),cmp);
- //output
- fout<<topn[0].freq<<endl;
- fout<<Int2String(topn[0].num,topn[0].len);
- int n = 1;
- int perline = 1;
- for (int i = 1 ; i<size_top ;i++)
- {
- if (topn[i].freq < topn[i-1].freq)
- {
- perline= 1;
- n++;
- fout<<endl;
- if (n==N+1||topn[i].freq==0)
- {
- break;
- }
- fout<<topn[i].freq<<endl;
- fout<<Int2String(topn[i].num,topn[i].len);
- }
- else
- {
- if (perline%6==0)
- {
- fout<<endl;
- perline = 0;
- }
- else
- fout<<' ';
- fout<<Int2String(topn[i].num,topn[i].len);
- perline++;
- }
- }
- fin.close();
- fout.close();
- return 0;
- }
Add Comment
Please, Sign In to add comment