Skip to content

Commit c79bd3f

Browse files
committed
add
1 parent 266d9fa commit c79bd3f

File tree

8 files changed

+229
-1
lines changed

8 files changed

+229
-1
lines changed

20.8

35.2 KB
Binary file not shown.

20.8.cpp

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,60 @@
11
#include <iostream>
2+
#include <cstring>
23
using namespace std;
34

5+
class Trie{
6+
public:
7+
static const int MAX_N = 100 * 100;// 100为主串长度
8+
static const int CLD_NUM = 26;
9+
int size;
10+
int trie[MAX_N][CLD_NUM];
11+
12+
Trie(const char* s);
13+
void insert(const char* s);
14+
bool find(const char* s);
15+
};
16+
17+
Trie::Trie(const char* s){
18+
memset(trie[0], -1, sizeof(trie[0]));
19+
size = 1;
20+
while(*s){
21+
insert(s);
22+
++s;
23+
}
24+
}
25+
26+
void Trie::insert(const char* s){
27+
int p = 0;
28+
while(*s){
29+
int i = *s - 'a';
30+
if(-1 == trie[p][i]){
31+
memset(trie[size], -1, sizeof(trie[size]));
32+
trie[p][i] = size++;
33+
}
34+
p = trie[p][i];
35+
++s;
36+
}
37+
}
38+
39+
bool Trie::find(const char* s){
40+
int p = 0;
41+
while(*s){
42+
int i = *s - 'a';
43+
if(-1 == trie[p][i])
44+
return false;
45+
p = trie[p][i];
46+
++s;
47+
}
48+
return true;
49+
}
50+
451
int main(){
5-
cout<<"hey man! Watch your time!"<<endl;
52+
Trie tree("mississippi");
53+
string patt[] = {
54+
"is", "sip", "hi", "sis", "mississippa"
55+
};
56+
int n = 5;
57+
for(int i=0; i<n; ++i)
58+
cout<<tree.find((char*)&patt[i][0])<<endl;
659
return 0;
760
}

20.9

102 KB
Binary file not shown.

20.9.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include <iostream>
2+
#include <algorithm>
3+
#include <queue>
4+
#include <cstdlib>
5+
using namespace std;
6+
7+
class Median{
8+
private:
9+
priority_queue<int,vector<int>,less<int> > max_heap;//左边的数
10+
priority_queue<int,vector<int>,greater<int> > min_heap;//右边的数
11+
12+
public:
13+
void Insert(int v);
14+
int GetValue();
15+
};
16+
17+
void Median::Insert(int v){
18+
if(max_heap.empty() && min_heap.empty())
19+
max_heap.push(v);
20+
else if(!max_heap.empty() && min_heap.empty())
21+
max_heap.push(v);
22+
else if(max_heap.empty() && !min_heap.empty())
23+
min_heap.push(v);
24+
else{
25+
if(v < max_heap.top())
26+
max_heap.push(v);
27+
else
28+
min_heap.push(v);
29+
}
30+
//调整,保证两个堆的元素数量差别不大于1
31+
//不要用hmax_heap.size()-min_heap.size()>1
32+
//因为size返回的是unsigned类型,当左边相减得到一个负数时,本来为false
33+
//但会被转为一个大的正数,结果为true,出问题
34+
while(max_heap.size() > min_heap.size()+1){
35+
int data = max_heap.top();
36+
min_heap.push(data);
37+
max_heap.pop();
38+
}
39+
while(min_heap.size() > max_heap.size()+1){
40+
int data = min_heap.top();
41+
max_heap.push(data);
42+
min_heap.pop();
43+
}
44+
}
45+
46+
int Median::GetValue(){//中位数为int,由于有除法,也可改为float
47+
if(max_heap.empty() && min_heap.empty())
48+
return (1<<31); //都为空时,返回int最小值
49+
if(max_heap.size() == min_heap.size())
50+
return (max_heap.top()+min_heap.top()) / 2;
51+
else if(max_heap.size() > min_heap.size())
52+
return max_heap.top();
53+
else
54+
return min_heap.top();
55+
}
56+
57+
int main(){
58+
srand((unsigned)time(0));
59+
Median md;
60+
vector<int> vi;
61+
int num = rand() % 30; //数量是30以内的随机数
62+
for(int i=0; i<num; ++i){
63+
int data = rand() % 100; //元素是100内的数
64+
vi.push_back(data);
65+
md.Insert(data);
66+
}
67+
sort(vi.begin(), vi.end());
68+
for(int i=0; i<num; ++i)
69+
cout<<vi.at(i)<<" "; //排序的序列
70+
cout<<endl<<md.GetValue()<<endl; //中位数
71+
return 0;
72+
}

3.6

0 Bytes
Binary file not shown.

3.6.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ stack<int> Ssort(stack<int> s){
2929
}
3030
return t;
3131
}
32+
3233
int main(){
3334
srand((unsigned)time(0));
3435
stack<int> s;

ac_automation.cpp

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#include <iostream>
2+
#include <queue>
3+
#include <cstring>
4+
using namespace std;
5+
6+
class ACAutomation{
7+
public:
8+
static const int MAX_N = 1000 * 50;
9+
static const int CLD_NUM = 26;
10+
11+
int size;
12+
int fail[MAX_N];
13+
int tag[MAX_N];
14+
int trie[MAX_N][CLD_NUM];
15+
16+
void reset();
17+
void insert(const char* s);
18+
void construct();
19+
int query(const char* s);
20+
};
21+
22+
void ACAutomation::reset(){
23+
memset(trie[0], -1, sizeof(trie[0]));
24+
tag[0] = 0;
25+
size = 1;
26+
}
27+
28+
void ACAutomation::insert(const char* s){
29+
int p = 0;
30+
while(*s){
31+
int i = *s - 'a';
32+
if(-1 == trie[p][i]){
33+
memset(trie[size], -1, sizeof(trie[size]));
34+
tag[size] = 0;
35+
trie[p][i] = size++;
36+
}
37+
p = trie[p][i];
38+
++s;
39+
}
40+
++tag[p];
41+
}
42+
43+
void ACAutomation::construct(){
44+
queue<int> q;
45+
fail[0] = 0;
46+
for(int i=0; i<CLD_NUM; ++i){
47+
if(-1 != trie[0][i]){
48+
fail[trie[0][i]] = 0;
49+
q.push(trie[0][i]);
50+
}
51+
else
52+
trie[0][i] = 0;
53+
}
54+
while(!q.empty()){
55+
int u = q.front();
56+
q.pop();
57+
for(int i=0; i<CLD_NUM; ++i){
58+
int &v = trie[u][i];
59+
if(-1 != v){
60+
q.push(v);
61+
fail[v] = trie[fail[u]][i];
62+
//tag[u] += tag[fail[u]];
63+
}
64+
else
65+
v = trie[fail[u]][i];
66+
}
67+
}
68+
}
69+
70+
//返回匹配的模式串个数
71+
int ACAutomation::query(const char* s){
72+
int p = 0, cnt = 0;
73+
while(*s){
74+
int i = *s - 'a';
75+
while(-1==trie[p][i] && p!=0)//无法匹配当前字符,回退到其fail指针
76+
p = fail[p];
77+
p = trie[p][i];
78+
p = p==-1 ? 0 : p;
79+
int t = p;
80+
while(t!=0 && tag[t]!=-1){
81+
cnt += tag[t];
82+
tag[t] = -1;
83+
t = fail[t];//跳到当前字符的最大后缀,统计模式串出现个数
84+
}
85+
++s;
86+
}
87+
return cnt;
88+
}
89+
90+
int main(){
91+
ACAutomation ac;
92+
ac.reset();
93+
string patt[] = {
94+
"is", "sip", "is", "sis", "mississipp"
95+
};
96+
int n = 5;
97+
for(int i=0; i<n; ++i)
98+
ac.insert((char*)&patt[i][0]);
99+
ac.construct();
100+
cout<<ac.query("mississippi")<<endl;
101+
return 0;
102+
}

heap.h

Whitespace-only changes.

0 commit comments

Comments
 (0)