Skip to content

Commit d4a2c41

Browse files
author
Jie Feng
committed
haha
1 parent fdadb99 commit d4a2c41

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+473078
-671
lines changed

SortOnPerm.class

1.12 KB
Binary file not shown.

SortOnPerm.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
public class SortOnPerm {
2+
public static void main(String[] arg){
3+
int[] x = {0,3,5,1,4,2};//{0,17,5,1,9};
4+
int[] a = {0,4,2,3,1,5};//{0,3,2,4,1};
5+
6+
mergeSort(a,x,1,a.length-1);
7+
for(int i=1;i<x.length;i++)
8+
System.out.print(x[i]+" ");
9+
}
10+
11+
public static void mergeSort(int[] a,int[] x,int low,int high){
12+
if(low>=high)
13+
return;
14+
int mid = (low+high)/2;
15+
mergeSort(a,x,low,mid);
16+
mergeSort(a,x,mid+1,high);
17+
merge(a,x,low,mid,high);
18+
}
19+
20+
public static void merge(int[] a,int[] x,int low,int mid ,int high){
21+
for(int i=low,j=mid+1;i<=high&&j<=high;j++){
22+
if(i<=high && j<=high){
23+
if(a[j]<a[i]){
24+
int tmp = x[i];
25+
x[i] = x[j];
26+
x[j] = tmp;
27+
tmp = a[i];
28+
a[i] =a[j];
29+
a[j] = tmp;
30+
i++;j--;
31+
}
32+
}
33+
}
34+
}
35+
}

algorithm-puzzle/endianness.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
int main(){
5+
char a[4];
6+
int *b = (int*)a;
7+
*b = 1;
8+
if (a[0]) cout << "Little Endian" << endl;
9+
else cout << "Big Endian" << endl;
10+
}

algorithm-puzzle/stuph.tar.gz

794 KB
Binary file not shown.

algorithm-puzzle/stuph/makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
target= solution
2+
3+
all:
4+
#g++ -o $(target) -g $(target).cpp -w
5+
g++ -o $(target) -g $(target).cpp -w -std=c++11
6+
7+
.PHONY: run debug
8+
9+
run:
10+
./${target}
11+
12+
debug:
13+
gdb ${target}

algorithm-puzzle/stuph/out

Lines changed: 8 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#include<iostream>
2+
#include<ctime>
3+
#include<vector>
4+
#include<set>
5+
#include<string>
6+
#include<fstream>
7+
using namespace std;
8+
#define F(i,L,R) for (int i = L; i < R; i++)
9+
#define FE(i,L,R) for (int i = L; i <= R; i++)
10+
#define printA(a,L,R) FE(i,L,R) cout << a[i] << (i==R?'\n':' ')
11+
#define printM(a,n,m) F(i,0,n){ F(j,0,m) cout << a[i][j] << ' '; cout << endl;}
12+
#define printV(a) printA(a,0,a.size()-1);
13+
14+
15+
class Trie{
16+
public:
17+
bool word;
18+
Trie* next[26];
19+
Trie(){
20+
word = false;
21+
for (int i = 0; i < 26; i++){
22+
next[i] = NULL;
23+
}
24+
}
25+
void insert(string& s, int p){
26+
if (p == s.length()){
27+
word = true;
28+
return;
29+
}
30+
31+
int i = s[p] - 'a';
32+
if (next[i] == NULL)
33+
next[i] = new Trie();
34+
next[i]->insert(s, p+1);
35+
}
36+
};
37+
38+
class solution{
39+
private:
40+
vector<string> words;
41+
Trie* root;
42+
string text;
43+
int maxMatch;
44+
double runTime;
45+
public:
46+
solution(){
47+
getText();
48+
getWords();
49+
}
50+
void run10times(){
51+
auto t1 = clock();
52+
for (int i = 0; i < 10; i++){
53+
buildTrie();
54+
solve();
55+
}
56+
auto t2 = clock();
57+
runTime = (t2-t1) * 1.0/ CLOCKS_PER_SEC;
58+
output();
59+
}
60+
void output(){
61+
cout << maxMatch << endl;
62+
printf("Characters used: %d (%0.2f%%)\n", maxMatch, maxMatch*100.0/text.length());
63+
cout << "Output:\n" << text << endl << endl <<
64+
"Benchmarking.........." << endl;
65+
printf("average time per run: %.4lf ms (10 runs, %.2lf ms total)\n", runTime/10, runTime);
66+
}
67+
void solve(){
68+
vector<int> f(text.length()+1, 0), last(text.length()+1, -1);
69+
int i = 0;
70+
for (int i = 0; i < text.length(); i++){
71+
Trie* cur = root;
72+
int j = i;
73+
while(j < text.length() && cur->next[text[j]-'a'] != NULL){
74+
cur = cur->next[text[j]-'a'];
75+
j++;
76+
if (cur->word){
77+
if (f[i] + (j-i) > f[j]){
78+
f[j] = f[i] + j-i;
79+
last[j] = i;
80+
}
81+
}
82+
}
83+
}
84+
for (int i = 1; i <= text.length(); i++){
85+
f[i] = f[i-1];
86+
if (last[i] != -1)
87+
f[i] = max(f[last[i]] + i-last[i], f[i]);
88+
}
89+
i = text.length();
90+
int miss = 0;
91+
while(i >= 1){
92+
if (f[i] == f[i-1]) text[i-1] = '-', i--, miss++;
93+
else {
94+
i = last[i];
95+
}
96+
}
97+
maxMatch = text.length() - miss;
98+
}
99+
void getWords(){
100+
ifstream fin("words.txt");
101+
string s;
102+
while(fin >> s){
103+
words.push_back(s);
104+
}
105+
}
106+
void buildTrie(){
107+
root = new Trie();
108+
for (int i = 0; i < words.size(); i++){
109+
root->insert(words[i], 0);
110+
}
111+
}
112+
void getText(){
113+
ifstream fin("stuph.txt");
114+
//ifstream fin("stuph_sample.txt");
115+
fin >> text;
116+
}
117+
};
118+
int main(){
119+
solution s;
120+
s.run10times();
121+
}

algorithm-puzzle/stuph/stuph.txt

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
AceAteTentEsth-Lop----Aer--IlaLesSicUplaArm-OmarGinDid-AbuPoly---Tost-OseLin---Pad-Dice-Den---Aby---
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
aceatetentestholophwdgaerctilalessicuplaarmmomargindidrabupolycaitostioselinirtpadidicendenopiabynos

0 commit comments

Comments
 (0)