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 25, 2019
1 parent d4c90bb commit e437387
Show file tree
Hide file tree
Showing 6 changed files with 382 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 = "cellphoneTyping_UVA12526"; //*
const string CPPfile = "freeParentheses_UVA1238"; //*
//***************************************

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

using namespace std;

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

const int N = 3000;

void solve(int i, int openC, int sum, vector<int> & isPositive, vector<int> & a, set<int> & res, vector<vector<vector<int>>> & dp)
{
int n = a.size();

if (dp[i][openC][sum + N]) return;

if (i == n - 1)
{
res.insert(sum);
return;
}

int add = a[i + 1] * isPositive[i + 1] * ((openC % 2 == 0) ? 1 : -1);

if (isPositive[i + 1] == -1) solve(i + 1, openC + 1, sum + add, isPositive, a, res, dp);
if (openC > 0) solve(i + 1, openC - 1, sum + add, isPositive, a, res, dp);
solve(i + 1, openC, sum + add, isPositive, a, res, dp);

dp[i][openC][sum + N] = 1;
}

int main()
{
while (true)
{
string s = ""; getline(fin, s);
if (s == "") break;

s = "+ " + s;

vector<int> isPositive, a;
istringstream intputS(s);
while (true)
{
char ch = '_'; string n_s = ""; intputS >> ch >> n_s;
if (n_s == "") break;

if (ch == '+') isPositive.push_back(1);
else isPositive.push_back(-1);

if (ch == '+') a.push_back(stoi(n_s));
else a.push_back(stoi(n_s));
}

int n = a.size();
set<int> res;
vector<vector<vector<int>>> dp(n, vector<vector<int>>(n, vector<int>(N * 2 + 1, 0)));
solve(0, 0, a[0], isPositive, a, res, dp);

fout << res.size() << '\n';
}

return 0;
}
100 changes: 100 additions & 0 deletions myCpps/freeParentheses_UVA1238.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
61 - 65 - 6 + 25 - 63 + 97 - 92 + 10 - 25 + 48 - 45 - 57 - 38 + 11 - 86 - 64 - 51 + 23 - 14 + 14 + 22 - 22 - 75 - 45 + 6 + 6 + 28
18 + 41 + 77 - 57 - 24
99 - 17 + 99 - 67 + 46 - 40 - 74 - 26 - 83 + 88 + 47 + 34 + 14
23 - 5 + 83 - 95
47 - 83 - 21 + 76 - 4 - 51 + 34 - 15 - 2 - 70 - 55
76 + 8 + 72 - 87 - 44 + 67 - 41 + 57 + 53 + 71 + 44 + 96 - 32 - 73 + 87 + 86 - 57 + 44 + 90 - 1 - 38 + 61 + 8 - 0 + 84
28 + 80 - 87 + 65 - 14 + 90 + 91 - 51 + 80 - 71 + 75 + 50 + 92 - 60 - 73 - 15 - 73 - 59 - 60 + 73 + 29 - 32 + 14 + 60 - 25
52 + 45 - 41
1 + 30 - 46 + 59 - 46 - 83 + 47 - 85 + 72 + 52 + 1 - 34 + 49 - 30 - 94 + 84 + 25 + 52 - 65 + 94 - 86 + 69 + 87 + 65 - 24
77 - 83 - 55 - 47 - 30 + 10 + 31 + 82 + 59 + 99 - 10 + 35 - 23 + 86 + 86 - 7 - 40 - 21 - 26
8 - 14 + 23 + 83 - 33 - 8 - 0 + 22 - 2 - 10 - 87 - 51 - 32 + 88 - 40 - 26 - 74 - 22 + 90 + 59 - 52 - 7 - 86
40 + 28 + 29 - 49 - 61 + 76 - 39 + 15 + 94 - 10 - 91 + 26 - 75 - 19 + 82 + 64 - 98 + 27 - 7 - 63 - 10 - 69 - 8 + 67 + 78 + 71 + 29 - 72
91 + 39 + 1 + 35 - 0 + 30 + 90
1 + 33 - 3 - 86 - 97 - 52 + 85 - 70 + 90 + 53 - 59 - 36 - 5 + 60
50 - 29 - 3 - 25 + 8 + 29 + 79
17 - 66 - 27 - 49 - 89 - 35 - 6 - 66 - 28 + 21 - 90 - 23 - 83 - 83 + 93 + 97 + 32 + 75 + 24 + 91 - 85
44 - 71 + 62 + 24 + 60 + 60 + 35 - 71 + 59 - 33 + 40 - 75 + 76 - 93 - 65 - 98
69 + 22 + 20 - 76 - 66
53 + 83 - 23 + 56 + 2 - 74 + 17 - 29 - 23 + 44 + 7 + 43 + 12 - 80 - 48 - 77 - 51 + 7 - 7 + 36
10 - 87 + 65 - 96 - 63 - 10 + 11 + 4 - 68 - 16 + 31 - 84 - 57 - 61 + 8 + 74 + 60 + 7 + 82 - 14 - 95 + 72 - 81 + 34 - 61 - 18
54 + 30 - 46 - 21 + 89 + 31 - 13 + 75 + 45 + 45 - 69 - 0 - 46 + 51 + 89
36 - 76 - 93 + 78 + 81 - 62 + 69 - 55 - 23 - 89 - 41 + 90 + 60 + 80 + 0 - 37 + 5 + 94 - 39
72 - 43 - 37 - 8 + 60 + 92 - 3 - 11 + 79 + 87 + 87 - 80 - 95 + 89 - 89 + 65 + 53 - 87 + 73 - 82 - 78 - 87 + 64
44 + 74 + 55 - 58 - 89 - 26 + 47 + 11
64 + 22 + 54 - 9 + 33 + 14 + 73 + 24 - 27 - 76 + 64 + 70 - 93 - 84 + 63 + 53 + 59 + 99 - 75 - 53 + 99
73 + 50 + 30 + 8 - 12 - 46 + 77 + 83 - 31 + 8 + 36 + 64 + 32
37 - 48 + 57 + 71 - 39 + 85 + 13 + 71 - 83 - 50 + 62 - 65 + 18 + 90 - 40 + 22 - 12 + 37 + 4
95 + 63 - 42 + 62 - 87 + 13 + 64 + 18 + 46 - 82 - 24 - 62
74 + 21 + 95 - 12 + 72 - 37 - 55 - 31 - 50 - 31 + 72 - 96 - 90 - 58 - 5 - 13 - 47 - 73 + 63 + 66
56 + 1 - 8 - 65 - 28 - 76 - 98 - 70 - 55 - 7 - 1 - 30 - 58 + 90 + 93 - 8 + 13 + 81 + 45 + 35 + 95
45 - 36 + 38 + 73 - 3 + 69 + 74 - 41 + 52 - 73 - 45 - 73 - 11 - 77 + 91 + 58 - 70 - 62 + 89 - 5 - 13 + 29 + 53 + 3 + 6
51 - 15 + 81
59 - 73 + 84 - 73 + 81 - 15 - 74 + 66 - 95 - 78 - 67 + 6 + 11 - 76 + 70 - 5 + 68 - 90 - 66 - 7 - 3 - 70 + 94 + 69 - 33 - 90
96 + 25 + 23 + 19 + 62 + 28 - 8 - 83 - 82 - 22 + 16
11 + 82 + 52 - 85 + 19 - 42 - 60 - 1 + 11 + 9 + 46 - 58 - 91 - 24 + 30 - 11 + 4 - 36 + 12 + 3 + 41 + 31 - 63 + 37 - 47
94 - 15 + 76 - 65 + 25 - 93 + 94 - 33 + 58 + 3 - 80 + 10 - 2 + 43 - 1 + 38 + 36 - 40 + 12 + 13 - 4 - 25 - 8 - 32 - 55 + 83 + 36 - 98 + 19 - 30
68 - 40 - 73 - 85 - 41 + 27 + 8 - 0 - 15 + 86 + 72 + 10 + 63 - 51 + 54 - 7 + 1 + 15 - 31 - 17 - 2 - 21 + 1
17 + 6 - 92 - 87 - 91 + 15 - 36 + 76 - 64 + 46 + 57
74 + 53 + 70 - 96 + 48 - 41 + 92 + 89 - 39 + 33 - 27 - 77 + 69 - 86 + 73 - 45 + 21 + 90 - 78 - 15 - 65 + 85 - 93 + 41 - 58
86 + 96
66 - 10 + 34 + 75 - 40 - 82 - 39 + 20 + 39 - 15 + 94 + 2 - 46 - 62 - 98 - 7 + 28 - 20 + 53 + 94 - 43 + 79 + 89 - 90 + 36
82 - 70 - 70 - 22 + 54 + 94 + 13 - 83 - 89 + 33 + 85 - 12 + 12 - 63 + 33 + 11 - 33 + 51 + 98 + 39 - 98 - 70 + 61 - 50 + 41 - 86 - 12 - 42 + 3 - 78
90 - 77 - 72 - 49 + 5 - 27 - 75 - 68 - 3 + 38 - 5 - 75 - 74 + 94
73 - 13 - 15 + 30 + 70 - 9 + 20 + 32 - 39 - 83
74 + 91 + 98 + 97 - 85 - 54 - 17 - 96 - 10 + 71
32 - 61 + 73 - 26 + 45 + 7 - 35 - 46 - 17 - 63 + 88 + 67 + 37 - 41 - 42 - 85 - 96
70 - 91 - 34 + 70 + 7 + 44 + 43 - 49 + 68 + 76 + 46 + 50 - 65 - 51 - 44 + 96 + 70 - 31 + 79 - 98 - 11 - 92 + 7 + 65 + 83 + 10
61 - 69 - 5 - 74 + 72 - 25 - 14 - 99 + 62 + 80 - 91 - 31 - 18 + 26 - 41 - 82 + 1 + 76
2 - 9 - 25 - 95 - 94 - 86 - 43 - 33 + 43 + 36 - 65 + 14 - 3 - 11 + 52 - 63 - 41 + 80 + 58 + 11 + 21 - 13
47 - 50 + 16 + 23 + 1 - 92 + 38 - 61 - 94 - 22
54 + 59 - 82 + 12 - 19 - 68 - 25 + 97 - 9 + 74 + 62 - 19
80 + 42 - 57 - 9 - 22 - 17 - 90 + 8 - 8 + 47 - 50 - 26 - 17 + 75 - 19 - 31 + 27 - 15 - 93 + 94 + 96 - 20
80 + 99 - 8 - 38 - 95 - 22 + 65 - 43 - 92 + 30 + 51 - 7 + 25 + 11 + 16 - 11 - 11 + 15
54 - 82 - 70 + 20 + 2 - 24 - 93 - 17 - 4 + 92 + 29 + 3 - 35 - 7 + 29 - 70 - 29 + 50 - 8 - 87 + 37 - 8
25 + 78 - 1 - 55 - 25 - 28 + 76 + 59 + 10 + 99 + 85 + 39 - 71 - 4 - 41 - 42 - 90 + 84 + 42 - 26 - 1 + 80 + 41
52 - 24 + 43 - 36 + 3 + 12 + 20 - 71 + 16 - 87 + 57 + 43 - 1 - 89 - 21 - 25 - 34 - 95 + 11 - 64
77 - 73
97 - 25 - 10 + 50 + 45 + 54 + 89 + 81 - 30 - 71 - 60 - 94 + 97 - 38 - 88 - 50 + 9
71 + 91 + 43 + 17 - 36 + 45 + 69 + 27 - 91 - 7 + 64 - 49 + 97 + 66
75 - 79 - 99 - 4 + 59 + 91 - 27 + 16 - 15 + 90 + 36 - 97 - 6 + 54 + 87 - 55 - 48 + 79 + 56
68 + 56 + 15 - 21 + 84 - 28 - 96 + 24 - 68 - 43 - 86 + 43 - 28 + 27 + 91 + 94 - 51 + 18 + 38 - 51 + 28 - 49 + 47 + 21 - 3 + 89 + 46
75 + 29 - 86 + 45 - 5 + 35 + 67 - 15 + 0 + 53 - 72 + 22
77 + 10 - 19 + 42 + 83
70 - 43 + 3
90 - 24 + 16 - 50 - 35 + 17 + 56 - 52 - 68 + 33 + 19 - 93 - 98 + 24 + 20 - 42 + 70 - 12 + 58 + 54 + 71 + 4 + 62 - 55 - 71 - 70 + 67 + 97 + 23
95 + 1 + 75 - 20 + 9 + 55 - 76 + 34 - 5 + 38 + 78 - 1
21 + 15 - 38 + 74 + 85 + 3 + 50 + 53 - 6 + 74 - 34 + 16 - 8 + 52
6 + 87 + 79 + 72 + 45 + 15 - 26 + 96 + 45 - 88 + 57 + 87 + 75 - 34 + 9 - 71 - 45 - 0 - 17 + 93 - 75 - 46 - 30 - 79 + 43 - 28 + 34 - 43 + 70 + 94
17 + 11 + 27 + 89 + 88 + 60 - 43 - 17 - 52 + 2 - 68 - 47 - 97 + 12 - 19 - 2 + 75 + 53 + 77 + 52 - 78 + 80 - 22 - 99 + 98 - 21
20 - 69 + 10 + 51 + 92 + 21 + 67 - 22 - 70 - 61
93 - 73 + 43 - 38 + 78 - 60 + 31 + 94 - 15 + 27 - 99 - 26 + 75 + 14 + 27 - 45
71 - 30 + 48 - 10 + 84 - 88 + 4 + 61 - 81 - 30
9 - 51 + 62 + 62 + 93 - 7 - 82 - 82 + 59 + 9 + 68 - 11 + 84 + 44
26 - 50 + 87 + 75 + 70 + 91 - 48 + 81 - 88 + 65 - 61 + 95 - 28 - 1 + 35 + 82 - 76 + 11 - 37 + 17 - 75 - 23 - 58 + 11
28 + 98 + 55 + 10 - 4 + 99 + 13 + 92 - 65 - 5 - 42 - 10 + 82 - 46 + 41 - 39 - 37 - 95 - 83 + 79 + 22 + 79 + 12 - 77 - 79 - 32
94 - 96 - 79 + 30 - 3 - 35 - 12 + 85 - 40 - 87 - 95 - 2 + 93 + 38 - 13
21 + 65 - 11 + 89 - 17 - 15 + 32 + 35 + 75
14 + 90 - 32 + 89 + 23 - 50 + 50
15 - 48 + 11 + 65 + 81 - 33 - 60 + 35 + 96 - 1 + 51 - 82 + 63 + 72 - 93 - 19 + 89 - 26 + 5 - 33 + 72 + 4 - 87 + 72
26 - 73 + 11 - 57 + 60 + 44 + 3 - 59 + 30 + 96 - 77 + 26 - 49 - 86 + 22 - 72 + 85 - 0 + 53 + 75 + 90 + 23 - 34 - 37 - 20 + 52 - 16
65 + 8 + 53 + 0 + 67 + 23 - 85 - 29 + 93 - 83 + 65 - 51 + 34 - 40
56 - 64 - 21 + 15 + 7 - 52
27 - 46 + 7 + 54 - 94 + 69 - 7 + 14 - 95 + 79 + 63 - 46 + 20 + 63 - 29 + 6 - 47 + 75 - 27 + 8 + 20 + 85 + 50
97 - 57 - 17 + 48 - 9 - 28 + 99 + 40 - 95 - 9 - 95 + 52 - 14
99 - 56 + 50 + 32 + 81 + 80 + 16 - 97 + 13 - 54 + 6 - 46 + 71 - 56 - 96 - 0 - 21 + 15 + 85 + 31 + 0 - 96 - 4
17 - 95 + 23 + 67 + 21 - 14 - 67 + 52 - 91 - 76 + 97 - 5 + 32
81 + 73 + 79 - 17 - 4 + 79 + 14 - 14 + 31 + 91 - 49 + 57 + 88 + 42 + 39 + 50 + 8 + 34 + 12 - 13 - 44 - 92 - 93
24 + 88 + 8 + 63 + 18 - 76 - 0 - 87 + 51 + 77 - 82 + 89 + 58 - 40 + 95 + 9 + 10 - 19
34 - 87 - 38 - 20 - 32 + 93 - 55 - 72 + 12 + 85 + 98 + 25 - 11 + 83 - 87 + 91 - 36 - 85
6 - 17 - 66 + 65 - 32 + 59 + 3 - 20 + 74 + 99
34 + 58 + 36 + 49 - 32 - 3 - 92 - 57 - 34 + 32 + 64 - 16 - 40 + 22 + 33 + 56 + 56 - 41 - 68 - 21 + 28
73 - 49 - 83 + 14 + 3 + 11 + 47 + 7 - 22 + 42 - 62 + 46 - 95 - 97 + 67 - 93 + 2 - 38 - 79 - 5 - 31 - 53 - 72 - 99
42 - 73 + 92 + 69 - 86 + 8 + 48 - 51 + 96 + 40 - 41 + 9 - 14 - 3 + 74 + 33 - 49 + 93 + 30 - 33 + 85 + 69 - 94 + 36 - 13 - 66
28 + 31 + 90 + 79 - 42 + 96 + 75 + 84
17 - 73 - 10 + 75 + 66 + 96 - 40 - 85 + 42 + 55 + 56 - 2 - 51 - 41 + 39 - 83 - 95 + 90 + 50 - 62 - 81 + 47 + 77 + 35 + 54 - 56
10 + 12 + 60 - 8 + 30 + 48 - 0 - 87 + 7 - 19 - 12 + 9 + 55 + 60 + 40 - 23 + 31 - 51 + 40 + 44 - 55 + 52 + 34 + 57 - 41 + 82 + 99 - 42 - 40 - 68
43 + 2 - 31 + 99 + 69 - 46 + 39 + 31
41 + 18 - 21 - 36 - 96 - 75 + 85 - 21
75 + 15 - 33 + 93 + 61 - 79 + 90 + 29
0 + 64 + 52 - 98
100 changes: 100 additions & 0 deletions myCpps/freeParentheses_UVA1238.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
948
2
348
3
155
1046
1076
1
1170
690
842
1210
3
514
26
1028
456
2
509
1078
331
837
1295
15
776
86
611
87
717
906
998
2
1296
16
631
1037
675
166
1103
1
1101
1399
488
100
28
619
1210
758
843
56
202
735
482
713
943
651
1
711
113
866
1044
92
3
2
1251
66
131
1044
932
26
498
92
327
1035
983
560
36
7
1090
1121
89
11
844
419
782
306
757
406
820
123
601
1012
1046
4
1288
1074
13
27
13
1
65 changes: 65 additions & 0 deletions myCpps/freeParentheses_UVA1238OJ.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <bits/stdc++.h>

using namespace std;


const int N = 3000;

void solve(int i, int openC, int sum, vector<int> & isPositive, vector<int> & a, set<int> & res, vector<vector<vector<int>>> & dp)
{
int n = a.size();

if (dp[i][openC][sum + N]) return;

if (i == n - 1)
{
res.insert(sum);
return;
}

int add = a[i + 1] * isPositive[i + 1] * ((openC % 2 == 0) ? 1 : -1);

if (isPositive[i + 1] == -1) solve(i + 1, openC + 1, sum + add, isPositive, a, res, dp);
if (openC > 0) solve(i + 1, openC - 1, sum + add, isPositive, a, res, dp);
solve(i + 1, openC, sum + add, isPositive, a, res, dp);

dp[i][openC][sum + N] = 1;
}

int main()
{
ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
while (true)
{
string s = ""; getline(cin, s);
if (s == "") break;

s = "+ " + s;

vector<int> isPositive, a;
istringstream intputS(s);
while (true)
{
char ch = '_'; string n_s = ""; intputS >> ch >> n_s;
if (n_s == "") break;

if (ch == '+') isPositive.push_back(1);
else isPositive.push_back(-1);

if (ch == '+') a.push_back(stoi(n_s));
else a.push_back(stoi(n_s));
}

int n = a.size();
set<int> res;
vector<vector<vector<int>>> dp(n, vector<vector<int>>(n, vector<int>(N * 2 + 1, 0)));
solve(0, 0, a[0], isPositive, a, res, dp);

cout << res.size() << '\n';
}

cout.flush();
return 0;
}

53 changes: 53 additions & 0 deletions myCpps/freeParentheses_UVA1238_AC.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@

#include <bits/stdc++.h>
using namespace std;

int num[35], sign[35], cnt;
int memo[35][35][6500];
set<int> S;

void dp(int open, int n, int value)
{
if (memo[open][n][value + 3200] != -1)
return;
if (n == cnt - 1)
{
S.insert(value);
return;
}
int nval = sign[n + 1] * num[n + 1] * (open % 2 == 0 ? 1 : -1);
if (open > 0)
dp(open - 1, n + 1, value + nval);
if (sign[n + 1] == -1)
dp(open + 1, n + 1, value + nval);
dp(open, n + 1, value + nval);
memo[open][n][value + 3200] = 0;
}

int main()
{
freopen("freeParentheses_UVA1238.in", "r", stdin);
freopen("freeParentheses_UVA1238.out", "w", stdout);

string s;
char c;
while (getline(cin, s))
{
stringstream sin;
sin << s;
sin >> num[0];
S.clear();
sign[0] = 1;
cnt = 1;
while (sin >> c)
{
sign[cnt] = c == '-' ? -1 : 1;
sin >> num[cnt];
cnt++;
}
memset(memo, -1, sizeof memo);
dp(0, 0, num[0]);
printf("%d\n", (int)S.size());
}
return 0;
}

0 comments on commit e437387

Please sign in to comment.