Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
gzshawnliang committed Aug 17, 2019
1 parent 5265530 commit ee60eeb
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 1 deletion.
2 changes: 1 addition & 1 deletion myCpps/!-OJcreater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
using namespace std;

//***************************************
const string CPPfile = "palinwords_UVA257"; //*
const string CPPfile = "fewestFlops_UVA11552"; //*
//***************************************

ifstream fin(CPPfile + ".cpp");
Expand Down
78 changes: 78 additions & 0 deletions myCpps/fewestFlops_UVA11552.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include <bits/stdc++.h>

using namespace std;

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

const int inf = INT_MAX / 3;

int ctoi(char ch)
{
return ch - 'a';
}

int main()
{
int ac = 26;
string alphabets = "abcdefghijklmnopqrstuvwxyz";

int tcc; fin >> tcc;
for (int t = 1; t <= tcc; ++t)
{
int k; fin >> k;
string s; fin >> s;

int n = s.size(), lastMin = inf, nowMin = inf;
vector<vector<int>> dp(n / k, vector<int>(ac, inf));
for (int i = 0; i <= n - 1; i += k)
{
string now = s.substr(i, k), allChar = "";
vector<int> count(ac + 1, 0);

for (int j = 0; j <= k - 1; ++j)
{
if (count[ctoi(now[j])] == 0)
{
allChar += now[j];
}
++count[ctoi(now[j])];
}

int tc = allChar.size();
if (i == 0)
{
for (int j = 0; j <= tc - 1; ++j)
{
dp[i / k][ctoi(allChar[j])] = tc;
}
lastMin = tc;
}
else
{
for (int p = 0; p <= tc - 1; ++p)
{
for (int q = 0; q <= tc - 1; ++q)
{
if (p == q && tc > 1) continue;

char head = allChar[p], tail = allChar[q];

if (dp[i / k][ctoi(tail)] == inf) dp[i / k][ctoi(tail)] = lastMin + tc;

dp[i / k][ctoi(tail)] = min(dp[i / k][ctoi(tail)], dp[i / k - 1][ctoi(head)] + tc - 1);

nowMin = min(nowMin, dp[i / k][ctoi(tail)]);
}
}

lastMin = nowMin;
nowMin = inf;
}
}

fout << lastMin << '\n';
}

return 0;
}
13 changes: 13 additions & 0 deletions myCpps/fewestFlops_UVA11552.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
12
6 nuxxzrzbmnmgqooket
6 nuxxzrzbmnmgqooketlyhnko
6 nuxxzrzbmnmgqooketlyhnkoaugzqr
6 nuxxzrzbmnmgqooketlyhnkoaugzqrcddiut
6 nuxxzrzbmnmgqooketlyhnkoaugzqrcddiuteiojwa
6 nuxxzrzbmnmgqooketlyhnkoaugzqrcddiuteiojwayyzpvs
6 nuxxzrzbmnmgqooketlyhnkoaugzqrcddiuteiojwayyzpvscmpsaj
6 nuxxzrzbmnmgqooketlyhnkoaugzqrcddiuteiojwayyzpvscmpsajlfvgub
6 nuxxzrzbmnmgqooketlyhnkoaugzqrcddiuteiojwayyzpvscmpsajlfvgubfaaovl
6 nuxxzrzbmnmgqooketlyhnkoaugzqrcddiuteiojwayyzpvscmpsajlfvgubfaaovlzylntr
6 augzqr
6 augzqrcddiut
12 changes: 12 additions & 0 deletions myCpps/fewestFlops_UVA11552.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
14
19
25
29
34
39
44
50
54
59
6
10
37 changes: 37 additions & 0 deletions myCpps/fewestFlops_UVA11552DC.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <bits/stdc++.h>

using namespace std;

ofstream fout("fewestFlops_UVA11552.in");

int getRand(int a, int b)
{
int c = b - a + 1;
return rand() % c + a;
}

int main()
{
//string alphabets = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
string alphabets = "abcdefghijklmnopqrstuvwxyz";
int ac = alphabets.size();

int tcc = 1000, maxN = 10;
fout << tcc << '\n';

for (int t = 1; t <= tcc; ++t)
{
int k = getRand(2, 5);

string ans = "";
int n = getRand(1, maxN / k) * k;
for (int c = 1; c <= n; ++c)
{
ans.push_back(alphabets[getRand(0, ac - 1)]);
}

fout << k << ' ' << ans << '\n';
}

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

using namespace std;


const int inf = INT_MAX / 3;

int ctoi(char ch)
{
return ch - 'a';
}

int main()
{
ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
int ac = 26;
string alphabets = "abcdefghijklmnopqrstuvwxyz";

int tcc; cin >> tcc;
for (int t = 1; t <= tcc; ++t)
{
int k; cin >> k;
string s; cin >> s;

int n = s.size(), lastMin = inf, nowMin = inf;
vector<vector<int>> dp(n / k, vector<int>(ac, inf));
for (int i = 0; i <= n - 1; i += k)
{
string now = s.substr(i, k), allChar = "";
vector<int> count(ac + 1, 0);

for (int j = 0; j <= k - 1; ++j)
{
if (count[ctoi(now[j])] == 0)
{
allChar += now[j];
}
++count[ctoi(now[j])];
}

int tc = allChar.size();
if (i == 0)
{
for (int j = 0; j <= tc - 1; ++j)
{
dp[i / k][ctoi(allChar[j])] = tc;
}
lastMin = tc;
}
else
{
for (int p = 0; p <= tc - 1; ++p)
{
for (int q = 0; q <= tc - 1; ++q)
{
if (p == q && tc > 1) continue;

char head = allChar[p], tail = allChar[q];

if (dp[i / k][ctoi(tail)] == inf) dp[i / k][ctoi(tail)] = lastMin + tc;

dp[i / k][ctoi(tail)] = min(dp[i / k][ctoi(tail)], dp[i / k - 1][ctoi(head)] + tc - 1);

nowMin = min(nowMin, dp[i / k][ctoi(tail)]);
}
}

lastMin = nowMin;
nowMin = inf;
}
}

cout << lastMin << '\n';
}

cout.flush();
return 0;
}

0 comments on commit ee60eeb

Please sign in to comment.