-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_sort_by_key.cpp
More file actions
63 lines (59 loc) · 933 Bytes
/
string_sort_by_key.cpp
File metadata and controls
63 lines (59 loc) · 933 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
* string sort: first, sort by specified key with char sequence, then sort by a-z sequence
* e.g: "helloworld",key:"kol", result: oollldehrw
*/
include <iostream>
using namespace std;
bool isok(const char *skey,char ch)
{
while(*skey)
{
if(*skey==ch)
return true;
skey++;
}
return false;
}
char* sort_key(const char* src, const char* key)
{
const char *p=src,*q=key;
int hash[26]={0};
int cnt[26]={0};
int ival=0,ival2=0;
int len1=strlen(src);
int i=0,j=0;
char *dest=new char[len1+1];
char *t=dest;
while(*p)
{
hash[*p-'a']=++cnt[*p-'a'];
p++;
}
while(*q)
{
int tcnt=hash[*q-'a'];
if(tcnt)
{
for(i=0;i<tcnt;++i)
*dest++=*q;
}
q++;
}
for(i=0;i<26;++i)
{
int tcnt=hash[i];
if(false==isok(key,char(i+'a')))
{
for(j=0;j<tcnt;++j)
*dest++=char(i+'a');
}
}
*dest='\0';
return t;
}
int main()
{
char *d=sort_key("helloworld","kol");
cout<<d<<endl;
return 0;
}