Skip to content

Commit 1b30050

Browse files
committed
Add some java files
1 parent e08d48a commit 1b30050

File tree

9 files changed

+393
-0
lines changed

9 files changed

+393
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// The Genome Database of All Space Life
2+
import java.util.*;
3+
4+
public class Main {
5+
static String formula;
6+
static int pos;
7+
static int n;
8+
static StringBuilder sb;
9+
10+
public static void main(String[] args) {
11+
Scanner sc = new Scanner(System.in);
12+
while (true) {
13+
formula = sc.next();
14+
n = sc.nextInt();
15+
if (formula.equals("0") && n == 0)
16+
break;
17+
pos = 0;
18+
sb = new StringBuilder();
19+
parse();
20+
if (n >= sb.length())
21+
System.out.println(0);
22+
else
23+
System.out.println(sb.charAt(n));
24+
}
25+
sc.close();
26+
}
27+
28+
static void parse() {
29+
while (pos < formula.length() && sb.length() <= n + 1)
30+
term();
31+
}
32+
33+
static void term() {
34+
if (sb.length() > n + 1) {
35+
pos = formula.length();
36+
return;
37+
}
38+
if (Character.isUpperCase(formula.charAt(pos))) {
39+
if (sb.length() <= n + 1)
40+
sb.append(formula.charAt(pos));
41+
pos++;
42+
return;
43+
}
44+
int n = number();
45+
if (formula.charAt(pos) != '(') {
46+
for (int i = 0; i < n; i++)
47+
sb.append(formula.charAt(pos));
48+
pos++;
49+
return;
50+
}
51+
pos++;
52+
for (int i = 0; i < n - 1; i++) {
53+
int t = pos;
54+
while (pos < formula.length() && formula.charAt(pos) != ')')
55+
term();
56+
pos = t;
57+
}
58+
while (pos < formula.length() && formula.charAt(pos) != ')')
59+
term();
60+
pos++;
61+
return;
62+
}
63+
64+
static int number() {
65+
int result = 0;
66+
while (Character.isDigit(formula.charAt(pos))) {
67+
result *= 10;
68+
result += formula.charAt(pos++) - '0';
69+
}
70+
return result;
71+
}
72+
}

AizuOnlineJudge/ProblemSets11/1145_The_Genome_Database_of_All_Space_Life/README.md

Whitespace-only changes.
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// Bug Hunt
2+
import java.util.*;
3+
4+
public class Main {
5+
static String statement;
6+
static int pos;
7+
static Map<Character, Integer> sizes;
8+
static Map<Data, Integer> values;
9+
10+
public static void main(String[] args) {
11+
Scanner sc = new Scanner(System.in);
12+
out: while (true) {
13+
int result = 0;
14+
int counter = 1;
15+
sizes = new HashMap<Character, Integer>();
16+
values = new HashMap<Data, Integer>();
17+
while (true) {
18+
statement = sc.next();
19+
pos = 0;
20+
if (statement.equals(".")) {
21+
if (counter != 1)
22+
break;
23+
break out;
24+
}
25+
if (!statement.contains("="))
26+
declaration();
27+
else if (!assignment()) {
28+
result = counter;
29+
break;
30+
}
31+
counter++;
32+
}
33+
System.out.println(result);
34+
while (!statement.equals("."))
35+
statement = sc.next();
36+
}
37+
sc.close();
38+
}
39+
40+
static void declaration() {
41+
char array = statement.charAt(pos++);
42+
pos++;
43+
int num = number();
44+
sizes.put(array, num);
45+
}
46+
47+
static boolean assignment() {
48+
char array = statement.charAt(pos++);
49+
if (!sizes.containsKey(array))
50+
return false;
51+
pos++;
52+
int n = -1;
53+
if (Character.isDigit(statement.charAt(pos)))
54+
n = number();
55+
else
56+
n = expression();
57+
if (n == -1 || n >= sizes.get(array))
58+
return false;
59+
pos++;
60+
Data data = new Data(array, n);
61+
pos++;
62+
int r = expression();
63+
if (r == -1)
64+
return false;
65+
values.put(data, r);
66+
return true;
67+
}
68+
69+
static int number() {
70+
int result = 0;
71+
while (pos < statement.length()
72+
&& Character.isDigit(statement.charAt(pos))) {
73+
result *= 10;
74+
result += statement.charAt(pos++) - '0';
75+
}
76+
return result;
77+
}
78+
79+
static int expression() {
80+
if (Character.isDigit(statement.charAt(pos)))
81+
return number();
82+
char name = statement.charAt(pos++);
83+
pos++;
84+
int n = -1;
85+
if (Character.isDigit(statement.charAt(pos)))
86+
n = number();
87+
else
88+
n = expression();
89+
if (n == -1)
90+
return -1;
91+
Data data = new Data(name, n);
92+
pos++;
93+
if (values.containsKey(data))
94+
return values.get(data);
95+
return -1;
96+
}
97+
98+
static class Data {
99+
char name;
100+
int num;
101+
102+
Data(char na, int nu) {
103+
name = na;
104+
num = nu;
105+
}
106+
107+
@Override
108+
public boolean equals(Object anotherData) {
109+
return this.name == ((Data) anotherData).name
110+
&& this.num == ((Data) anotherData).num;
111+
}
112+
113+
@Override
114+
public int hashCode() {
115+
return new Character(name).hashCode() + new Integer(num).hashCode();
116+
}
117+
}
118+
}

AizuOnlineJudge/ProblemSets12/1282_Bug_Hunt/README.md

Whitespace-only changes.
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
// Lost Number
2+
import java.util.*;
3+
4+
public class Main {
5+
static String formula;
6+
static int pos;
7+
8+
public static void main(String[] args) {
9+
Scanner sc = new Scanner(System.in);
10+
formula = sc.next();
11+
int result = replaceAndSearch();
12+
System.out.println(result);
13+
sc.close();
14+
}
15+
16+
static int replaceAndSearch() {
17+
String[] replaces = { "0", "1", "+", "-", "*", "(", ")" };
18+
StringBuilder sb = new StringBuilder(formula);
19+
int result = -1;
20+
if (formula.contains(".")) {
21+
int p = formula.indexOf(".");
22+
for (int i = 0; i < replaces.length; i++) {
23+
formula = sb.replace(p, p + 1, replaces[i]).toString();
24+
result = Math.max(result, replaceAndSearch());
25+
formula = sb.replace(p, p + 1, ".").toString();
26+
}
27+
} else {
28+
pos = 0;
29+
if (isExp()) {
30+
result = exp();
31+
}
32+
}
33+
return result;
34+
}
35+
36+
static boolean isExp() {
37+
if (!isTerm())
38+
return false;
39+
while (pos < formula.length()) {
40+
char op = formula.charAt(pos++);
41+
if (op != '+' && op != '-' && op != '*')
42+
return false;
43+
if (!isTerm())
44+
return false;
45+
}
46+
pos = 0;
47+
return true;
48+
}
49+
50+
static boolean isTerm() {
51+
if (pos >= formula.length())
52+
return false;
53+
char c = formula.charAt(pos);
54+
if (c == '(') {
55+
pos++;
56+
return isParExp();
57+
}
58+
if (Character.isDigit(c)) {
59+
return isNumber();
60+
}
61+
return false;
62+
}
63+
64+
static boolean isParExp() {
65+
if (!isTerm())
66+
return false;
67+
if (pos >= formula.length())
68+
return false;
69+
char op = formula.charAt(pos++);
70+
if (op != '+' && op != '-' && op != '*')
71+
return false;
72+
if (!isTerm())
73+
return false;
74+
while (pos < formula.length() && formula.charAt(pos) != ')') {
75+
op = formula.charAt(pos++);
76+
if (op != '+' && op != '-' && op != '*')
77+
return false;
78+
if (!isTerm())
79+
return false;
80+
}
81+
if (pos >= formula.length() || formula.charAt(pos++) != ')')
82+
return false;
83+
return true;
84+
}
85+
86+
static boolean isNumber() {
87+
int result = 0;
88+
while (pos < formula.length() && Character.isDigit(formula.charAt(pos))) {
89+
result *= 2;
90+
result += formula.charAt(pos++) - '0';
91+
}
92+
return result >= 0 && result < 1024;
93+
}
94+
95+
static int exp() {
96+
int result = term();
97+
if (result < 0 || result >= 1024)
98+
return -1;
99+
while (pos < formula.length() && formula.charAt(pos) != ')') {
100+
char op = formula.charAt(pos++);
101+
if (op == '+') {
102+
int t = term();
103+
if (t < 0 || t >= 1024)
104+
return -1;
105+
result += t;
106+
} else {
107+
int t = term();
108+
if (t < 0 || t >= 1024)
109+
return -1;
110+
result -= t;
111+
}
112+
if (result < 0 || result >= 1024)
113+
return -1;
114+
}
115+
pos++;
116+
return result;
117+
}
118+
119+
static int term() {
120+
int result = mul();
121+
if (result < 0 || result >= 1024)
122+
return -1;
123+
while (pos < formula.length() && formula.charAt(pos) == '*') {
124+
pos++;
125+
int t = mul();
126+
if (t < 0 || t >= 1024)
127+
return -1;
128+
result *= t;
129+
if (result < 0 || result >= 1024)
130+
return -1;
131+
}
132+
return result;
133+
}
134+
135+
static int mul() {
136+
if (formula.charAt(pos) == '(') {
137+
pos++;
138+
return exp();
139+
}
140+
int n = number();
141+
if (n < 0 || n >= 1024)
142+
return -1;
143+
return n;
144+
}
145+
146+
static int number() {
147+
int result = 0;
148+
while (pos < formula.length() && Character.isDigit(formula.charAt(pos))) {
149+
result *= 2;
150+
result += formula.charAt(pos++) - '0';
151+
}
152+
return result;
153+
}
154+
155+
}

AizuOnlineJudge/ProblemSets24/2428_Lost_Number/README.md

Whitespace-only changes.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
public class GameOfSegments {
2+
3+
public int winner(int N) {
4+
if (game(N) % 2 == 1)
5+
return 1;
6+
return 2;
7+
}
8+
9+
int game(int N) {
10+
if (N <= 1)
11+
return 0;
12+
if (N % 2 == 1)
13+
return game((N - 2) / 2 + 1) + game((N - 2) / 2) + 1;
14+
return game((N - 2) / 2) * 2 + 1;
15+
}
16+
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import java.util.Arrays;
2+
3+
public class CostOfDancing {
4+
5+
public int minimum(int K, int[] danceCost) {
6+
Arrays.sort(danceCost);
7+
int sum=0;
8+
for(int i=0;i<K;i++)
9+
sum+=danceCost[i];
10+
return sum;
11+
}
12+
13+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import java.util.Arrays;
2+
3+
public class BuildingHeightsEasy {
4+
5+
public int minimum(int M, int[] heights) {
6+
Arrays.sort(heights);
7+
int result = Integer.MAX_VALUE;
8+
for (int i = M; i <= heights.length; i++) {
9+
int sum = 0;
10+
for (int j = i - M; j < i; j++) {
11+
sum += heights[i - 1] - heights[j];
12+
}
13+
result = Math.min(sum, result);
14+
}
15+
return result;
16+
}
17+
18+
}

0 commit comments

Comments
 (0)