#include<iostream>
//c039 3n+1
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int i, j;
while (cin >> i >> j) {
int m = min(i, j);
int M = max(i, j);
int cycle = 0;
for (int k = m; k <= M; k++) {
int n = k;
int t = 0;
while (1) {
t++;
if (n == 1) break;
if (n % 2) n = 3 * n + 1;
else n = n / 2;
}
cycle = max(cycle, t);
}
cout << i << " " << j << " " << cycle << endl;
}
return 0;
}
// c007 TEX
#include <iostream>
using namespace std;
int main() {
char c;
bool flag = true;
while (cin.get(c)) {
if (c == '"') {
cout << (flag ? "``" : "''");
flag = !flag;
}
else cout << c;
}
return 0;
}
// c054 WERTYU
#include <iostream>
using namespace std;
string t = "`1234567890-=QWERTYUIOP[]\ASDFGHJKL;'ZXCVBNM,./";
int main() {
char c;
while (cin.get(c)) {
if (c == ' ' || c == '\n')
cout << c;
else {
int p = t.find(c);
cout << t[p - 1];
}
}
return 0;
}
// e543
#include <iostream>
#include <algorithm>
#include <string>
#define ALL(x) x.begin(), x.end()
using namespace std;
string nt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
string mt = "A 3 HIL JM O 2TUVWXY51SE Z 8 ";
bool isP(string s);
bool isM(string s);
int main() {
string s;
while (cin >> s) {
bool p = isP(s);
bool m = isM(s);
cout << s << " -- is ";
if (p && m) cout << "a mirrored palindrome.";
else if (p) cout << "a regular palindrome.";
else if (m) cout << "a mirrored string.";
else cout << "not a palindrome.";
cout << "\n\n";
}
return 0;
}
bool isP(string s) {
string rs = s;
reverse(ALL(rs));
return rs == s;
}
bool isM(string s) {
string rs = s;
reverse(ALL(rs));
for (int i = 0; i < s.size(); i++) {
int p = nt.find(rs[i]);
rs[i] = mt[p];
}
return rs == s;
}
// d132
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int game = 0;
int n;
while (cin >> n && n != 0) {
printf("Game %d:\n", ++game);
vector<int> s(n);
for (int i = 0; i < n; i++) cin >> s[i];
vector<int> g(n);
while (true) {
int A = 0, B = 0, z = 0;
for (int i = 0; i < n; i++) {
cin >> g[i];
if (s[i] == g[i]) A++;
if (g[i] == 0) z++;
}
if (z == n) break;
for (int d = 1; d <= 9; d++) {
int sc = 0, gc = 0;
for (int i = 0; i < n; i++) {
if (s[i] == d) sc++;
if (g[i] == d) gc++;
}
B = B + min(sc, gc);
}
B = B - A;
printf(" (%d,%d)\n", A, B); // ?e??b???? 4 ??
}
}
return 0;
}
// e558
#include <iostream>
using namespace std;
int t[100001] = { 0 };
int main() {
for (int n = 1; n <= 100000; n++) {
int y = n, x = n;
while (x > 0) {
y = y + x % 10;
x = x / 10;
}
if (y > 100000) continue;
if (t[y] == 0) t[y] = n;
}
int T, m;
cin >> T;
while (T--) {
cin >> m;
cout << t[m] << endl;
}
return 0;
}
// b123
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int rs, cs;
while (cin >> rs >> cs) {
int arr[201][201];
for (int r = 0; r < rs; r++) {
for (int c = 0; c < cs; c++) {
cin >> arr[r][c];
if (arr[r][c] && c > 0)
arr[r][c] = arr[r][c - 1] + 1;
}
}
int A = 0;
for (int r = 0; r < rs; r++) {
for (int c = 0; c < cs; c++) {
int w = 201;
for (int h = 0; r >= h && arr[r - h][c]; h++) {
w = min(w, arr[r - h][c]);
A = max(A, w * (h + 1));
}
}
}
cout << A << endl;
}
return 0;
}
// j121
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#define ALL(x) x.begin(), x.end()
using namespace std;
int main() {
string sa, sb;
while (cin >> sa >> sb) {
vector<int> va(26, 0), vb(26, 0);
for (auto c : sa) va[c - 'A']++;
for (auto c : sb) vb[c - 'A']++;
sort(ALL(va));
sort(ALL(vb));
cout << (va == vb ? "YES" : "NO") << endl;
}
return 0;
}
// e541
#include <iostream>
#include <vector>
#include <algorithm>
#define ALL(x) x.begin(), x.end()
using namespace std;
int main() {
int kase = 0;
int N, Q;
while (cin >> N >> Q && N != 0) {
printf("CASE# %d:\n", ++kase);
vector<int> v(N);
for (int n = 0; n < N; n++) cin >> v[n];
sort(ALL(v));
while (Q--) {
int f;
cin >> f;
int p = lower_bound(ALL(v), f) - v.begin();
if (p < N && v[p] == f)
printf("%d found at %d\n", f, (p + 1));
else
printf("%d not found\n", f);
}
}
return 0;
}
// c073
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int n;
vector<int> v[25];
void fb(int b, int &p, int &h);
void ca(int p, int h);
void po(int p0, int h, int p1);
void show();
int main() {
string s1, s2;
int a, b, pa, pb, ha, hb;
while (cin >> n) {
for (int i = 0; i < n; i++) {
v[i].clear();
v[i].push_back(i);
}
while (cin >> s1 && s1 != "quit") {
cin >> a >> s2 >> b;
fb(a, pa, ha);
fb(b, pb, hb);
if (pa != pb) {
if (s1 == "move") ca(pa, ha);
if (s2 == "onto") ca(pb, hb);
po(pa, ha, pb);
}
}
show();
}
return 0;
}
void fb(int b, int &p, int &h) {
for (p = 0; p < n; p++) {
for (h = 0; h < v[p].size(); h++) {
if (v[p][h] == b) return;
}
}
}
void ca(int p, int h) {
for (int i = v[p].size() - 1; i > h; i--) {
int b = v[p][i];
v[b].push_back(b);
v[p].pop_back();
}
return;
}
void po(int p0, int h, int p1) {
v[p1].insert(v[p1].end(), v[p0].begin() + h, v[p0].end());
v[p0].erase(v[p0].begin() + h, v[p0].end());
}
void show() {
for (int i = 0; i < n; i++) {
cout << i << ":";
for (int j = 0; j < v[i].size(); j++) {
cout << " " << v[i][j];
}
cout << endl;
}
}
// e155
#include <iostream>
#include <queue>
using namespace std;
int main() {
int n;
bool first;
while (cin >> n && n != 0) {
queue<int> q;
for (int i = 1; i <= n; i++) q.push(i);
cout << "Discarded cards: ";
first = true;
while (q.size() >= 2) {
if (first) first = false;
else cout << ", ";
cout << q.front(); q.pop();
q.push(q.front()); q.pop();
}
cout << endl;
cout << "Remaining card: " << q.front() << endl;
}
return 0;
}
// f166
#include <iostream>
#include <queue>
using namespace std;
int S[1000000];
int dis[1000000];
int main() {
int n, P, L, R;
queue<int> q;
int s, sL, sR;
cin >> n >> P >> L >> R;
for (int i = 0; i < n; i++) cin >> S[i];
q.push(0);
while (true) {
if (q.empty()) {
cout << -1;
break;
}
s = q.front(); q.pop();
if (s == P) {
cout << dis[s];
break;
}
sL = s - L;
if (sL >= 0) {
sL = S[sL];
if (sL >= 0 && sL < n && dis[sL] == 0) {
dis[sL] = dis[s] + 1;
q.push(sL);
}
}
sR = s + R;
if (sR < n) {
sR = S[sR];
if (sR >= 0 && sR < n && dis[sR] == 0) {
dis[sR] = dis[s] + 1;
q.push(sR);
}
}
}
return 0;
}
// b838
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
string s;
cin >> s;
stack<char> up;
bool valid = true;
int ans = 0;
for (auto c : s) {
if (c == '(') up.push('(');
else {
if (!up.empty() && up.top() == '(') {
ans++;
up.pop();
}
else {
valid = false;
break;
}
}
}
if (!up.empty()) valid = false;
cout << (valid ? ans : 0) << endl;
}
return 0;
}
// h028
#include <iostream>
#include <stack>
#include <algorithm>
#include <climits>
using namespace std;
pair<int, int> T[100002];
int main() {
int N, L;
int total = 0, height = 0;
stack<pair<int, int>> s;
cin >> N >> L;
T[0].first = 0;
T[0].second = INT_MAX;
for (int i = 1; i <= N; i++) cin >> T[i].first;
for (int i = 1; i <= N; i++) cin >> T[i].second;
T[N + 1].first = L;
s.push(T[0]);
for (int i = 1; i <= N; i++) {
bool left = T[i].first - T[i].second >= s.top().first;
bool right = T[i].first + T[i].second <= T[i + 1].first;
if (left || right) {
total++;
height = max(height, T[i].second);
while (s.top().first + s.top().second <= T[i + 1].first) {
total++;
height = max(height, s.top().second);
s.pop();
}
}
else {
s.push(T[i]);
}
}
cout << total << endl << height;
return 0;
}
// d217
#include <iostream>
#include <string>
#include <set>
using namespace std;
#define ALL(x) x.begin(), x.end()
int main() {
int round;
while (cin >> round && round != -1) {
string s, g;
int lc = 7;
cin >> s >> g;
printf("Round %d\n", round);
set<char> uc(ALL(s));
set<char> ag;
for (int i = 0; i < g.size(); i++) {
if (ag.count(g[i])) continue;
ag.insert(g[i]);
if (uc.count(g[i])) uc.erase(g[i]);
else lc--;
if (!lc || uc.empty()) break;
}
if (uc.empty()) cout << "You win.\n";
else if (!lc) cout << "You lose.\n";
else cout << "You chickened out.\n";
}
return 0;
}
// f698
#include <iostream>
#include <string>
#include <stack>
using namespace std;
int main() {
string ops = "+-*/";
stack<int> num;
string s;
while (cin >> s) {
int op = ops.find(s);
if (op == -1)
num.push(stoi(s));
else {
int n2 = num.top(); num.pop();
int n1 = num.top(); num.pop();
switch (op) {
case 0:
num.push(n1 + n2); break;
case 1:
num.push(n1 - n2); break;
case 2:
num.push(n1 * n2); break;
default:
num.push(n1 / n2);
}
}
}
cout << num.top();
return 0;
}
// d244
#include <iostream>
#include <map>
using namespace std;
int main() {
map<int, int> st;
int n;
while (cin >> n) st[n]++;
for (auto p : st) {
if (p.second % 3) {
cout << p.first;
break;
}
}
return 0;
}
// e564
#include <iostream>
#include <string>
#include <map>
#include <queue>
using namespace std;
int main() {
int t, n, x;
int t0;
int kase = 0;
while (cin >> t && t) {
map<int, int> team;
for (int i = 0; i < t; i++) {
cin >> n;
while (n--) {
cin >> x;
team[x] = i;
}
}
queue<int> tQueue;
queue<int> eQueue[1000];
printf("Scenario #%d\n", ++kase);
string cmd;
while (cin >> cmd && cmd != "STOP") { //?r??STOP??????B?z?R?O
if (cmd == "ENQUEUE") { // ENQUEUE: ?[?J???????
cin >> x;
t0 = team[x]; // ???o?i?J????????? ?ζ??s??
if (eQueue[t0].empty()) // ???? ?ζ??s?? ???b????????
tQueue.push(t0); // ?ζ??s?? ?[?J???????
eQueue[t0].push(x); // ?????s?? ?[?J???????
}
else { // DEQUEUE: ???}???????
t0 = tQueue.front(); // ???o?Y?N?????????? ?ζ??s??
cout << eQueue[t0].front() << "\n";//?L?X?????????s??
eQueue[t0].pop(); // ?R?????? ?????s??
if (eQueue[t0].empty()) // ?p?? ?ζ??s?? ??????????
tQueue.pop(); // ?R?????? ?ζ??s??
}
}
cout << "\n";
}
return 0;
}
// e548
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
int main() {
int n;
while (cin >> n) {
bool ans[] = { true, true, true };
stack<int> s;
queue<int> q;
priority_queue<int> pq;
while (n--) {
int t, x;
cin >> t >> x;
if (t == 1) {
s.push(x);
q.push(x);
pq.push(x);
}
else {
if (ans[0]) {
if (!s.empty() && x == s.top()) s.pop();
else ans[0] = false;
}
if (ans[1]) {
if (!q.empty() && x == q.front()) q.pop();
else ans[1] = false;
}
if (ans[2]) {
if (!pq.empty() && x == pq.top()) pq.pop();
else ans[2] = false;
}
}
}
if (ans[0] && !ans[1] && !ans[2])
cout << "stack\n";
else if (!ans[0] && ans[1] && !ans[2])
cout << "queue\n";
else if (!ans[0] && !ans[1] && ans[2])
cout << "priority queue\n";
else if (!ans[0] && !ans[1] && !ans[2])
cout << "impossible\n";
else
cout << "not sure\n";
}
return 0;
}
// d221
#include <iostream>
#include <queue>
using namespace std;
int main() {
int N, d;
while (cin >> N && N) {
priority_queue<int, vector<int>, greater<int>> pq;
for (int i = 0; i < N; i++) {
cin >> d;
pq.push(d);
}
long long sum, cost = 0;
while (pq.size() != 1) {
sum = pq.top(); pq.pop();
sum = sum + pq.top(); pq.pop();
cost = cost + sum;
pq.push(sum);
}
cout << cost << "\n";
}
return 0;
}
// h083
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
#define ALL(x) x.begin(), x.end()
int main() {
int m;
vector<string> v;
cin >> m;
while (m--) {
string s;
cin >> s;
v.push_back(s);
}
sort(ALL(v));
int ans = 0;
for (auto s : v) {
int len = s.size();
for (int i = 1; i <= len / 2; i++) {
string sa = s.substr(0, i);
string sc = s.substr(len - i, i);
if (sa == sc) {
string sb = s.substr(i, len - 2 * i);
if (binary_search(ALL(v), sb))
ans++;
}
}
}
cout << ans;
return 0;
}
// f640
#include <iostream>
#include <string>
using namespace std;
int func();
int main() {
cout << func() << "\n";
return 0;
}
int func() {
string token;
int x, y, z;
cin >> token;
if (token == "f") {
x = func();
return 2 * x - 3;
}
if (token == "g") {
x = func();
y = func();
return 2 * x + y - 7;
}
if (token == "h") {
x = func();
y = func();
z = func();
return 3 * x - 2 * y + z;
}
return stoi(token);
}
// f637
#include <iostream>
#include <string>
using namespace std;
int decode(int n);
string s;
int p;
int main() {
int n;
cin >> s;
cin >> n;
p = -1;
cout << decode(n) << "\n";
return 0;
}
int decode(int n) {
int black = 0;
p++;
if (s[p] == '0')
black = 0;
else if (s[p] == '1')
black = n * n;
else {
for (int i = 0; i <= 3; i++)
black = black + decode(n / 2);
}
return black;
}
// k733
#include <iostream>
#include <string>
using namespace std;
long long loop(int &head, int &tail);
string s0;
int tape = 0;
int main() {
int h, t;
long long walk = 0;
cin >> s0;
walk = loop(h, t);
walk = walk + abs(h - 10);
cout << walk;
return 0;
}
long long loop(int &head, int &tail) {
long long walk = 0, subwalk;
int num, h, t;
head = tail = -1;
while (1) {
if (tape == s0.size()) return walk;
if (s0[tape] == 'T') {
num = stoi(s0.substr(tape + 1, 2));
tape = tape + 3;
if (tail == -1)
head = num;
else
walk = walk + abs(num - tail);
tail = num;
}
else if (s0[tape] == 'L') {
num = stoi(s0.substr(tape + 1, 1));
tape = tape + 2;
subwalk = loop(h, t);
if (tail == -1)
head = h;
else
walk = walk + abs(h - tail);
tail = t;
walk = walk + subwalk * num + abs(t - h) * (num - 1);
}
else {
tape++;
return walk;
}
}
}