Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
gzshawnliang committed Sep 13, 2019
1 parent 518780d commit 00dbef4
Show file tree
Hide file tree
Showing 7 changed files with 374 additions and 62 deletions.
77 changes: 15 additions & 62 deletions myCpps/kmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,87 +5,40 @@ using namespace std;
ifstream fin("powerStrings_UVA10298.in");
ofstream fout("powerStrings_UVA10298.out");

vector<int> createNext(string & P)
{
vector<int> next;
return next;
}

vector<int> KmpSearch(string & T,string & P)
{
vector<int> pos;
vector<int> next = createNext(P);

//begin search

return pos;
}

bool KmpExistSearch(string & T,string & P)
{
bool isExisted;
vector<int> next = createNext(P);

//begin search

return isExisted;
}

int main()
{
// string s = "ababababababababcabababab",
// t = "abababc";

string s = "krfxmjtbbbrywsrndqengumtgnbmpvzdsjw",
t = "tbbry";
string s = "ercaabcabcjjjj",
t = "abcabc";

int sizeS = s.size(), sizeT = t.size();

int _i = 1, _j = 0;
vector<int> next(sizeT, 0); next[0] = 0;
int _i = 0, _j = -1;
vector<int> next(sizeT + 1, 0); next[0] = -1;
while (_i <= sizeT - 1)
{
while (_j > 0 && t[_i] != t[_j])
while (_j >= 0 && t[_i] != t[_j])
{
if (_j > 0) _j = next[_j - 1];

if (_j == -1) break;
_j = next[_j];
}

if (t[_i] == t[_j]) next[_i] = _j + 1;

++_i;
++_j;
++_i; ++_j;
next[_i] = _j;
}

int i = 0, j = 0;
vector<int> ans;
while (i <= sizeS - 1)
{
if (s[i] != t[j])
while (j >= 0 && s[i] != t[j])
{
if (j == 0)
{
++i;
}
else
{
j = next[j - 1];
}
j = next[j];
}
else

++i; ++j;
if (j == sizeT)
{
if (j == sizeT - 1)
{
ans.push_back(i - sizeT + 1);
++i;
j = 0;
}
else
{
++i;
++j;
}
ans.push_back(i - sizeT);
j = next[j];
}
}

Expand Down
46 changes: 46 additions & 0 deletions myCpps_2020/importantStuffs/kmp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <bits/stdc++.h>

using namespace std;

ifstream fin("powerStrings_UVA10298.in");
ofstream fout("powerStrings_UVA10298.out");

int main()
{
string s = "ercaabcabcjjjj",
t = "abcabc";

int sizeS = s.size(), sizeT = t.size();

int _i = 0, _j = -1;
vector<int> next(sizeT + 1, 0); next[0] = -1;
while (_i <= sizeT - 1)
{
while (_j >= 0 && t[_i] != t[_j])
{
_j = next[_j];
}

++_i; ++_j;
next[_i] = _j;
}

int i = 0, j = 0;
vector<int> ans;
while (i <= sizeS - 1)
{
while (j >= 0 && s[i] != t[j])
{
j = next[j];
}

++i; ++j;
if (j == sizeT)
{
ans.push_back(i - sizeT);
j = next[j];
}
}

return 0;
}
166 changes: 166 additions & 0 deletions myCpps_2020/secretWord_UVA12467/!-OJcreater.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <set>
#include <string>
#include <vector>

using namespace std;

//***************************************
const string CPPfile = "secretWord_UVA12467"; //*
//***************************************

ifstream fin(CPPfile + ".cpp");
ofstream fout(CPPfile + "OJ.cpp");

bool _isAlpha(char a)
{
return (a >= 'a' && a <= 'z') || (a >= 'A' && a <= 'Z');
}

int main()
{
bool findMain = false;
bool fastDone = false;
while (true)
{
string in = "I am Shawn.";
getline(fin, in);
int size = in.size();

if (findMain == true && fastDone == false)
{
fout << in << '\n';
fout << "\tios_base::sync_with_stdio(false);\n\tstd::cin.tie(NULL);\n";
fastDone = true;
continue;
}

if (in == "I am Shawn.")
{
break;
}
else if (in == "")
{
fout << '\n';
continue;
}
else if (in == "int main()")
{
findMain = true;
fout << "int main()\n";
continue;
}
else if (size >= 9)
{
int i = 0, SPACEcount = 0, TABcount = 0;
while (in[i] == ' ' || in[i] == '\t')
{
++i;
if (in[i] == ' ')
{
++SPACEcount;
}
else
{
++TABcount;
}
}

string front = "";
for (int j = i; j <= size - 1; ++j)
{
front.push_back(in[j]);
}

if (front == "return 0;")
{
for (int c = 1; c <= SPACEcount; ++c)
{
fout << ' ';
}
for (int c = 1; c <= TABcount; ++c)
{
fout << '\t';
}

fout << "cout.flush();\n";

for (int c = 1; c <= SPACEcount; ++c)
{
fout << ' ';
}
for (int c = 1; c <= TABcount; ++c)
{
fout << '\t';
}

fout << "return 0;\n";
continue;
}
}

if (size >= 8)
{
string front = "";

int i = 0;
while (in[i] == ' ' || in[i] == '\t')
{
++i;
}

front.push_back(in[i]);
front.push_back(in[i + 1]);
front.push_back(in[i + 2]);
front.push_back(in[i + 3]);
front.push_back(in[i + 4]);
front.push_back(in[i + 5]);
front.push_back(in[i + 6]);
front.push_back(in[i + 7]);

if (front == "ifstream" || front == "ofstream")
{
continue;
}
}
if (size >= 2)
{
int i = 0;
while (in[i] == ' ' || in[i] == '\t')
{
++i;
}

if (i <= size - 2)
{
if (in[i] == '/' && in[i + 1] == '/')
{
continue;
}
}
}
if (size > 2)

for (int i = 0; i <= size - 5; ++i)
{
string temp;
temp.push_back(in[i]);
temp.push_back(in[i + 1]);
temp.push_back(in[i + 2]);
if ((temp == "fin" && _isAlpha(in[i + 3]) == false) || (temp == "fou" && in[i + 3] == 't' && _isAlpha(in[i + 4]) == false))
{
in[i] = 'c';
}
}

fout << in << '\n';
}

return 0;
}
71 changes: 71 additions & 0 deletions myCpps_2020/secretWord_UVA12467/secretWord_UVA12467.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include <bits/stdc++.h>

using namespace std;

ifstream fin("secretWord_UVA12467.in");
ofstream fout("secretWord_UVA12467.out");

int main()
{
int tcc; fin >> tcc;
for (int c = 1; c <= tcc; ++c)
{
string s; fin >> s;
int sizeS = s.size();

int l = 1, r = sizeS;
while (l <= r)
{
int m = (l + r) / 2;

string t = s.substr(0, m); reverse(t.begin(), t.end());

int _i = 0, _j = -1;
vector<int> next(m + 1, 0); next[0] = -1;
while (_i <= m - 1)
{
while (_j >= 0 && t[_i] != t[_j])
{
_j = next[_j];
}

++_i; ++_j;
next[_i] = _j;
}


bool flag = false;
int i = 0, j = 0;
while (i <= sizeS - 1)
{
while (j >= 0 && s[i] != t[j])
{
j = next[j];
}

++i; ++j;
if (j == m)
{
flag = true;
j = next[j];

break;
}
}

if (flag == true)
{
l = m + 1;
}
else
{
r = m - 1;
}
}

string t = s.substr(0, r); reverse(t.begin(), t.end());
fout << t << '\n';
}

return 0;
}
2 changes: 2 additions & 0 deletions myCpps_2020/secretWord_UVA12467/secretWord_UVA12467.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1
listentonetsilthesilence
1 change: 1 addition & 0 deletions myCpps_2020/secretWord_UVA12467/secretWord_UVA12467.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
netsil
Loading

0 comments on commit 00dbef4

Please sign in to comment.