buggy_code
stringlengths
11
625k
fixed_code
stringlengths
17
625k
bug_type
stringlengths
2
4.45k
language
int64
0
8
token_count
int64
5
200k
while (n = gets.chomp.to_i) != 0 pa,pb = [0, 0] n.times do a,b = gets.chomp.split.map(&:to_i) if a > b pa += a + b elsif a == b pa += a pa += b elsif a < b pb += a + b end end puts "#{pa} #{pb}" end
while (n = gets.chomp.to_i) != 0 pa,pb = [0, 0] n.times do a,b = gets.chomp.split.map(&:to_i) if a > b pa += a + b elsif a == b pa += a pb += b elsif a < b pb += a + b end end puts "#{pa} #{pb}" end
[["-", 0, 121, 75, 759, 64, 749, 0, 755, 31, 22], ["+", 0, 121, 75, 759, 64, 749, 0, 755, 31, 22]]
4
81
while true n = gets.to_i break if n == 0 a,b = 0,0 n.times do ca,cb = gets.split.map{|s|s.to_i} ca == cb ? (a+=ca;b+=cb) : (ca>cb ? a += ca+cb : b += ca+cb) end puts"#{ca} #{cb}" end
while true n = gets.to_i break if n == 0 a,b = 0,0 n.times do ca,cb = gets.split.map{|s|s.to_i} ca == cb ? (a+=ca;b+=cb) : (ca>cb ? a += ca+cb : b += ca+cb) end puts"#{a} #{b}" end
[["-", 0, 652, 3, 4, 0, 557, 0, 284, 0, 22], ["+", 0, 652, 3, 4, 0, 557, 0, 284, 0, 22]]
4
83
while str = gets do num = str.chomp.to_i a = 0 b = 0 num.times do |n| x, y = gets.chomp.split(" ").map(&:to_i) if x > y a += x + y elsif x < y b += x + y else a += x b += y end end puts [a, b].join(" ") end
while str = gets do num = str.chomp.to_i break if num == 0 a = 0 b = 0 num.times do |n| x, y = gets.chomp.split(" ").map(&:to_i) if x > y a += x + y elsif x < y b += x + y else a += x b += y end end puts [a, b].join(" ") end
[["+", 0, 89, 8, 170, 0, 751, 8, 94, 0, 94], ["+", 0, 493, 0, 89, 8, 170, 0, 751, 0, 121], ["+", 0, 89, 8, 170, 0, 751, 15, 738, 31, 22], ["+", 0, 89, 8, 170, 0, 751, 15, 738, 17, 60], ["+", 0, 89, 8, 170, 0, 751, 15, 738, 12, 612]]
4
86
def main(): sum_a = 0 sum_b = 0 while True: n = input() if n == '0': return for i in range(int(n)): a,b = list(map(int, input().split())) if a > b: sum_a += a+b elif a < b: sum_b += a+b else: sum_a += a sum_b += b print(sum_a, sum_b) if __name__ == '__main__': main()
# coding: utf-8 # Your code here! def main(): while True: sum_a = 0 sum_b = 0 n = input() if n == '0': return for i in range(int(n)): a,b = list(map(int, input().split())) if a > b: sum_a += a+b elif a < b: sum_b += a+b else: sum_a += a sum_b += b print(sum_a, sum_b) if __name__ == '__main__': main()
[["+", 0, 656, 0, 14, 8, 196, 0, 52, 0, 89], ["+", 0, 656, 0, 14, 8, 196, 0, 52, 15, 146], ["+", 0, 656, 0, 14, 8, 196, 0, 52, 0, 102], ["-", 0, 656, 0, 14, 8, 196, 0, 52, 0, 89], ["-", 0, 656, 0, 14, 8, 196, 0, 52, 15, 146], ["-", 0, 656, 0, 14, 8, 196, 0, 52, 0, 102]]
5
101
while 1: n = input() if n == 0: break sum_a = 0 sum_b = 0 for i in range(n): a,b = map(int, raw_input().split()) if a > b: sum_a = sum_a + a + b elif a < b: sum_b = sum_b + a + b elif a == b: sum_a = sum_a + a sum_b = sum_b + b print("%s %s" %(sum_a,sum_b))
while 1: n = int(input()) if n == 0: break sum_a = 0 sum_b = 0 for i in range(n): a,b = map(int, input().split()) if a > b: sum_a = sum_a + a + b elif a < b: sum_b = sum_b + a + b elif a == b: sum_a = sum_a + a sum_b = sum_b + b print("%s %s" %(sum_a,sum_b))
[["+", 8, 196, 0, 1, 0, 662, 12, 652, 63, 22], ["+", 0, 1, 0, 662, 12, 652, 3, 4, 0, 24], ["+", 0, 1, 0, 662, 12, 652, 3, 4, 0, 25], ["-", 3, 4, 0, 652, 63, 319, 500, 652, 63, 22], ["+", 3, 4, 0, 652, 63, 319, 500, 652, 63, 22]]
5
95
while True: n = eval(input()) if n == 0: break ATen = 0 BTen = 0 for i in range(n): A, B = list(map(int, input().split())) if A > B: ATen += A + B elif A < B: BTen += A + B elif A == B: ATen = A BTen = B print(ATen, BTen)
while True: n = eval(input()) if n == 0: break ATen = 0 BTen = 0 for i in range(n): A, B = list(map(int, input().split())) if A > B: ATen += A + B elif A < B: BTen += A + B elif A == B: ATen += A BTen += B print(ATen, BTen)
[["-", 75, 665, 64, 196, 0, 1, 0, 662, 0, 32], ["+", 75, 665, 64, 196, 0, 1, 0, 677, 17, 107]]
5
87
while True: n = int(input()) if n == 0: break score = {"A": 0, "B": 0} for _ in range(n): A, B = map(int, input().split()) if A > B: score["A"] += A elif A < B: score["B"] += B else: score["A"] += A score["B"] += B print(score["A"], score["B"])
while True: n = int(input()) if n == 0: break score = {"A": 0, "B": 0} for _ in range(n): A, B = map(int, input().split()) if A > B: score["A"] += A+B elif A < B: score["B"] += A+B else: score["A"] += A score["B"] += B print(score["A"], score["B"])
[["+", 64, 196, 0, 1, 0, 677, 12, 657, 17, 72], ["+", 64, 196, 0, 1, 0, 677, 12, 657, 12, 22], ["+", 64, 196, 0, 1, 0, 677, 12, 657, 31, 22]]
5
116
n = int(input()) while n != 0: A = 0 B = 0 for i in range(n): a, b = map(int, input().split()) if a > b: A += a + b elif a < b: B += a + b else: A += a B += b print("{0} {1}".format(A, B)) n = int(input())
n = int(input()) while n != 0: A = 0 B = 0 for i in range(n): a, b = map(int, input().split()) if a > b: A += (a + b) elif a < b: B += (a + b) else: A += a B += b print("{0} {1}".format(A, B)) n = int(input())
[["+", 64, 196, 0, 1, 0, 677, 12, 23, 0, 24], ["+", 64, 196, 0, 1, 0, 677, 12, 23, 0, 25]]
5
92
while 1: n=int(input()) if n==0: break a=b=0 while n: c,d=map(int,input().split()) if c<b:b+=c+d elif c>d:a+=c+d else: a+=c b+=c n-=1 print(a,b)
while 1: n=int(input()) if n==0: break a=b=0 while n: c,d=map(int,input().split()) if c<d:b+=c+d elif c>d:a+=c+d else: a+=c b+=c n-=1 print(a,b)
[["-", 0, 52, 8, 196, 0, 57, 15, 666, 0, 22], ["+", 0, 52, 8, 196, 0, 57, 15, 666, 0, 22]]
5
78
import sys def calc(): while True: n = int(input()) if n <= 0: break a = 0 b = 0 while n > 0: ans = list(map(int, input().split())) if ans[0] > ans[1]: a += ans[0] + ans[1] elif ans[1] < ans[0]: b += ans [0] + ans[1] else: a += ans[0] b += ans[1] n -= 1 print(a, b) if __name__ == '__main__': calc()
import sys def calc(): while True: n = int(input()) if n <= 0: break a = 0 b = 0 while n > 0: ans = list(map(int, input().split())) if ans[0] > ans[1]: a += ans[0] + ans[1] elif ans[0] < ans[1]: b += ans [0] + ans[1] else: a += ans[0] b += ans[1] n -= 1 print(a, b) if __name__ == '__main__': calc()
[["-", 0, 57, 75, 665, 15, 666, 0, 206, 206, 612], ["+", 0, 57, 75, 665, 15, 666, 0, 206, 206, 612]]
5
129
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.HashMap; import java.util.InputMismatchException; import java.util.Map; public class Main { MyScanner sc; PrintWriter writer; public void solve() { int n = sc.nextInt(); if (n == 0) { return; } Map<Character, Character> map = new HashMap<Character, Character>(n); for (int i = 0; i < n; i++) { map.put(sc.nextChar(), sc.nextChar()); } StringBuilder sb = new StringBuilder(); int m = sc.nextInt(); for (int i = 0; i < m; i++) { char c = sc.nextChar(); if (map.containsKey(c)) { sb.append(map.get(c)); } else { sb.append(c); } } writer.println(sb); } public Main() { sc = new MyScanner(System.in); writer = new PrintWriter(System.out); } public static void main(String[] args) { Main m = new Main(); m.solve(); m.writer.flush(); } class MyScanner { InputStream is; private byte[] buffer = new byte[1024]; private int bufferPointer = 0; private int bufferLength = 0; MyScanner(InputStream is) { this.is = is; } private int readByte() { if (bufferLength == -1) { throw new InputMismatchException(); } if (bufferPointer >= bufferLength) { bufferPointer = 0; try { bufferLength = is.read(buffer); } catch (IOException e) { throw new InputMismatchException(); } if (bufferLength <= -1) { return -1; } } return buffer[bufferPointer++]; } String next() { int b = skip(); StringBuilder sb = new StringBuilder(); while (isChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } char nextChar() { int b = skip(); return (char)b; } double nextDouble() { return Double.parseDouble(next()); } int nextInt() { int num = 0; int b; boolean negative = false; while ((b = readByte()) != -1 && !isDigit(b) && b != '-') ; if (b == '-') { negative = true; b = readByte(); } while (true) { if (isDigit(b)) { num = num * 10 + (b - '0'); } else { return negative ? -num : num; } b = readByte(); } } int[] nextIntArray(int length) { int[] array = new int[length]; for (int i = 0; i < length; i++) { array[i] = sc.nextInt(); } return array; } long nextLong() { long num = 0; int b; boolean negative = false; while ((b = readByte()) != -1 && !isDigit(b) && b != '-') ; if (b == '-') { negative = true; b = readByte(); } while (true) { if (isDigit(b)) { num = num * 10 + (b - '0'); } else { return negative ? -num : num; } b = readByte(); } } // from '!' to '~' private boolean isChar(int b) { return b >= 33 && b <= 126; } private boolean isDigit(int b) { return b >= '0' && b <= '9'; } private int skip() { int b; while ((b = readByte()) != -1 && !isChar(b)) ; return b; } } }
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.HashMap; import java.util.InputMismatchException; import java.util.Map; public class Main { MyScanner sc; PrintWriter writer; public void solve() { while (true) { int n = sc.nextInt(); if (n == 0) { return; } Map<Character, Character> map = new HashMap<Character, Character>(n); for (int i = 0; i < n; i++) { map.put(sc.nextChar(), sc.nextChar()); } StringBuilder sb = new StringBuilder(); int m = sc.nextInt(); for (int i = 0; i < m; i++) { char c = sc.nextChar(); if (map.containsKey(c)) { sb.append(map.get(c)); } else { sb.append(c); } } writer.println(sb); } } public Main() { sc = new MyScanner(System.in); writer = new PrintWriter(System.out); } public static void main(String[] args) { Main m = new Main(); m.solve(); m.writer.flush(); } class MyScanner { InputStream is; private byte[] buffer = new byte[1024]; private int bufferPointer = 0; private int bufferLength = 0; MyScanner(InputStream is) { this.is = is; } private int readByte() { if (bufferLength == -1) { throw new InputMismatchException(); } if (bufferPointer >= bufferLength) { bufferPointer = 0; try { bufferLength = is.read(buffer); } catch (IOException e) { throw new InputMismatchException(); } if (bufferLength <= -1) { return -1; } } return buffer[bufferPointer++]; } String next() { int b = skip(); StringBuilder sb = new StringBuilder(); while (isChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } char nextChar() { int b = skip(); return (char)b; } double nextDouble() { return Double.parseDouble(next()); } int nextInt() { int num = 0; int b; boolean negative = false; while ((b = readByte()) != -1 && !isDigit(b) && b != '-') ; if (b == '-') { negative = true; b = readByte(); } while (true) { if (isDigit(b)) { num = num * 10 + (b - '0'); } else { return negative ? -num : num; } b = readByte(); } } int[] nextIntArray(int length) { int[] array = new int[length]; for (int i = 0; i < length; i++) { array[i] = sc.nextInt(); } return array; } long nextLong() { long num = 0; int b; boolean negative = false; while ((b = readByte()) != -1 && !isDigit(b) && b != '-') ; if (b == '-') { negative = true; b = readByte(); } while (true) { if (isDigit(b)) { num = num * 10 + (b - '0'); } else { return negative ? -num : num; } b = readByte(); } } // from '!' to '~' private boolean isChar(int b) { return b >= 33 && b <= 126; } private boolean isDigit(int b) { return b >= '0' && b <= '9'; } private int skip() { int b; while ((b = readByte()) != -1 && !isChar(b)) ; return b; } } }
[["+", 8, 498, 0, 195, 8, 196, 0, 52, 0, 89], ["+", 0, 195, 8, 196, 0, 52, 15, 15, 0, 24], ["+", 0, 195, 8, 196, 0, 52, 15, 15, 0, 146], ["+", 0, 195, 8, 196, 0, 52, 15, 15, 0, 25], ["+", 0, 195, 8, 196, 0, 52, 8, 196, 0, 45], ["+", 0, 195, 8, 196, 0, 52, 8, 196, 0, 46]]
3
812
import java.io.PrintWriter; import java.util.Scanner; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); while (sc.hasNext()) { int m = sc.nextInt(); if (m == 0) break; String temp[][] = new String[2][m]; for (int i = 0; i < m; i++) { temp[0][i] = sc.next(); temp[1][i] = sc.next(); } int n = sc.nextInt(); String ans = ""; for (int i = 0; i < n; i++) { ans = sc.next(); for (int j = 0; j < m; j++) { if ((temp[0][j].equals(ans))) ans = temp[1][j]; } out.print(ans); } out.println(); out.flush(); } } }
import java.io.PrintWriter; import java.util.Scanner; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); while (sc.hasNext()) { int m = sc.nextInt(); if (m == 0) break; String temp[][] = new String[2][m]; for (int i = 0; i < m; i++) { temp[0][i] = sc.next(); temp[1][i] = sc.next(); } int n = sc.nextInt(); String ans = ""; for (int i = 0; i < n; i++) { ans = sc.next(); for (int j = 0; j < m; j++) { if ((temp[0][j].equals(ans))) { ans = temp[1][j]; break; } } out.print(ans); } out.println(); out.flush(); } } }
[["+", 0, 7, 8, 196, 0, 57, 64, 196, 0, 45], ["+", 8, 196, 0, 57, 64, 196, 0, 1, 0, 35], ["+", 8, 196, 0, 57, 64, 196, 0, 93, 0, 94], ["+", 0, 7, 8, 196, 0, 57, 64, 196, 0, 46]]
3
240
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n; int roop = 0; StringBuilder[] ans = new StringBuilder[1000]; String tmpData; while ((n = sc.nextInt()) != 0) { String[][] str = new String[n][2]; for (int j = 0; j < n; j++) { //変換表の入力 str[j][0] = sc.next(); str[j][1] = sc.next(); } int dn = sc.nextInt(); //変換データ文字数 for (int j = 0; j < dn; j++) { tmpData = sc.next(); for (int k = 0; k < n; k++) { if (tmpData.equals(str[k][0])) { tmpData = str[k][1]; break; } } if (dn == 0) { ans[roop] = new StringBuilder(0); } else { ans[roop].append(dn); } } roop++; } sc.close(); for (int i = 0; i < roop; i++) { System.out.println(ans[roop].toString()); } } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n; int roop = 0; StringBuilder[] ans = new StringBuilder[1000]; String tmpData; while ((n = sc.nextInt()) != 0) { String[][] str = new String[n][2]; for (int j = 0; j < n; j++) { str[j][0] = sc.next(); str[j][1] = sc.next(); } int dn = sc.nextInt(); for (int j = 0; j < dn; j++) { tmpData = sc.next(); for (int k = 0; k < n; k++) { if (tmpData.equals(str[k][0])) { tmpData = str[k][1]; break; } } if (j == 0) { ans[roop] = new StringBuilder(tmpData); } else { ans[roop].append(tmpData); } } roop++; } sc.close(); for (int i = 0; i < roop; i++) { System.out.println(ans[i].toString()); } } }
[["-", 8, 196, 0, 57, 15, 15, 0, 16, 31, 22], ["+", 8, 196, 0, 57, 15, 15, 0, 16, 31, 22], ["-", 0, 1, 0, 11, 12, 230, 3, 4, 0, 499], ["+", 0, 1, 0, 11, 12, 230, 3, 4, 0, 22], ["-", 75, 196, 0, 1, 0, 492, 3, 4, 0, 22], ["+", 75, 196, 0, 1, 0, 492, 3, 4, 0, 22], ["-", 0, 492, 3, 4, 0, 492, 500, 504, 71, 22], ["+", 0, 492, 3, 4, 0, 492, 500, 504, 71, 22]]
3
286
import java.util.*; class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n; HashMap<String, String> hm = new HashMap<String, String>(); while (true) { n = scan.nextInt(); if (n == 0) { break; } for (int i = 0; i < n; i++) { hm.put(scan.next(), scan.next()); } n = scan.nextInt(); for (int i = 0; i < n; i++) { String s = scan.next(); String t = hm.get(s); if (t == null) { System.out.print(s); } else { System.out.print(t); } } System.out.println(); } } }
import java.util.*; class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n; HashMap<String, String> hm = new HashMap<String, String>(); while (true) { n = scan.nextInt(); if (n == 0) { break; } for (int i = 0; i < n; i++) { hm.put(scan.next(), scan.next()); } n = scan.nextInt(); for (int i = 0; i < n; i++) { String s = scan.next(); String t = hm.get(s); if (t == null) { System.out.print(s); } else { System.out.print(t); } } System.out.println(); hm.clear(); } } }
[["+", 0, 52, 8, 196, 0, 1, 0, 492, 500, 22], ["+", 0, 52, 8, 196, 0, 1, 0, 492, 0, 131], ["+", 0, 52, 8, 196, 0, 1, 0, 492, 141, 22], ["+", 8, 196, 0, 1, 0, 492, 3, 4, 0, 24], ["+", 8, 196, 0, 1, 0, 492, 3, 4, 0, 25], ["+", 8, 196, 0, 52, 8, 196, 0, 1, 0, 35]]
3
192
#include <stdio.h> char conv[10000]; char ans[100000005]; int main(void) { int i, j, n, m; char c, a, b; while (1) { scanf("%d", &n); if (n == 0) break; for (i = 0; i < n; i++) conv[i] = 0; for (i = 0; i < n; i++) { scanf(" %c %c", &a, &b); conv[a] = b; } scanf("%d", &m); for (i = 0; i < m; i++) { scanf(" %c", &c); if (conv[c] != 0) { ans[i] = conv[c]; } else { ans[i] = c; } } ans[i] = '\0'; printf("%s\n", ans); } return (0); }
#include <stdio.h> char conv[10000]; char ans[100000005]; int main(void) { int i, j, n, m; char c, a, b; while (1) { scanf("%d", &n); if (n == 0) break; for (i = 0; i < 10000; i++) conv[i] = 0; for (i = 0; i < n; i++) { scanf(" %c %c", &a, &b); conv[a] = b; } scanf("%d", &m); for (i = 0; i < m; i++) { scanf(" %c", &c); if (conv[c] != 0) { ans[i] = conv[c]; } else { ans[i] = c; } } ans[i] = '\0'; printf("%s\n", ans); } return (0); }
[["-", 0, 52, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 12, 13]]
0
206
#include <stdio.h> #include <string.h> int main(void) { int dice[6]; int i, j; int n; char str[5]; char str_t[6][6] = {"North", "East", "West", "South", "Right", "Left"}; int sum; int m; int tmp; while (1) { sum = 0; for (i = 0; i < 6; i++) { dice[i] = i + 1; } scanf("%d", &n); if (n == 0) { break; } for (i = 0; i < n; i++) { scanf("%s", str); for (j = 0; j < 6; j++) { if (!strcmp(str_t[j], str)) { m = j; break; } } switch (m) { case 0: tmp = dice[0]; dice[0] = dice[1]; dice[1] = dice[5]; dice[5] = dice[4]; dice[4] = tmp; sum += dice[0]; break; case 1: tmp = dice[0]; dice[0] = dice[3]; dice[3] = dice[5]; dice[5] = dice[2]; dice[2] = tmp; sum += dice[0]; break; case 2: tmp = dice[0]; dice[0] = dice[2]; dice[2] = dice[5]; dice[5] = dice[3]; dice[3] = tmp; sum += dice[0]; break; case 3: tmp = dice[0]; dice[0] = dice[4]; dice[4] = dice[5]; dice[5] = dice[1]; dice[1] = tmp; sum += dice[0]; break; case 4: tmp = dice[1]; dice[1] = dice[2]; dice[2] = dice[4]; dice[4] = dice[3]; dice[3] = tmp; sum += dice[0]; break; case 5: tmp = dice[1]; dice[1] = dice[3]; dice[3] = dice[4]; dice[4] = dice[2]; dice[2] = tmp; sum += dice[0]; break; } } printf("%d\n", sum); } return (0); }
#include <stdio.h> #include <string.h> int main(void) { int dice[6]; int i, j; int n; char str[5]; char str_t[6][6] = {"North", "East", "West", "South", "Right", "Left"}; int sum; int m; int tmp; while (1) { sum = 1; for (i = 0; i < 6; i++) { dice[i] = i + 1; } scanf("%d", &n); if (n == 0) { break; } for (i = 0; i < n; i++) { scanf("%s", str); for (j = 0; j < 6; j++) { if (!strcmp(str_t[j], str)) { m = j; break; } } switch (m) { case 0: tmp = dice[0]; dice[0] = dice[1]; dice[1] = dice[5]; dice[5] = dice[4]; dice[4] = tmp; sum += dice[0]; break; case 1: tmp = dice[0]; dice[0] = dice[3]; dice[3] = dice[5]; dice[5] = dice[2]; dice[2] = tmp; sum += dice[0]; break; case 2: tmp = dice[0]; dice[0] = dice[2]; dice[2] = dice[5]; dice[5] = dice[3]; dice[3] = tmp; sum += dice[0]; break; case 3: tmp = dice[0]; dice[0] = dice[4]; dice[4] = dice[5]; dice[5] = dice[1]; dice[1] = tmp; sum += dice[0]; break; case 4: tmp = dice[1]; dice[1] = dice[2]; dice[2] = dice[4]; dice[4] = dice[3]; dice[3] = tmp; sum += dice[0]; break; case 5: tmp = dice[1]; dice[1] = dice[3]; dice[3] = dice[4]; dice[4] = dice[2]; dice[2] = tmp; sum += dice[0]; break; } } printf("%d\n", sum); } return (0); }
[["-", 0, 52, 8, 9, 0, 1, 0, 11, 12, 13], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 12, 13]]
0
546
#include <stdio.h> int main(void) { int i, n, sum, dice[6], d; char inst[6]; scanf("%d", &n); while (n != 0) { sum = 1; dice[0] = 1; dice[1] = 2; dice[2] = 3; dice[3] = 4; dice[4] = 5; dice[5] = 6; for (i = 0; i < n; i++) { scanf("%s", inst); switch (inst[0]) { case 'S': d = dice[0]; dice[0] = dice[4]; dice[4] = dice[5]; dice[5] = dice[1]; dice[1] = d; break; case 'W': d = dice[0]; dice[0] = dice[2]; dice[2] = dice[5]; dice[5] = dice[3]; dice[3] = d; break; case 'E': d = dice[0]; dice[0] = dice[3]; dice[3] = dice[5]; dice[5] = dice[2]; dice[2] = d; break; case 'N': d = dice[0]; dice[0] = dice[1]; dice[1] = dice[5]; dice[5] = dice[4]; dice[4] = d; break; case 'L': d = dice[1]; dice[1] = dice[3]; dice[3] = dice[4]; dice[4] = dice[2]; dice[2] = d; break; case 'R': d = dice[1]; dice[1] = dice[2]; dice[2] = dice[4]; dice[4] = dice[3]; dice[3] = d; break; } sum += dice[0]; } printf("%d", sum); scanf("%d", &n); } return 0; }
#include <stdio.h> int main(void) { int i, n, sum, dice[6], d; char inst[6]; scanf("%d", &n); while (n != 0) { sum = 1; dice[0] = 1; dice[1] = 2; dice[2] = 3; dice[3] = 4; dice[4] = 5; dice[5] = 6; for (i = 0; i < n; i++) { scanf("%s", inst); switch (inst[0]) { case 'S': d = dice[0]; dice[0] = dice[4]; dice[4] = dice[5]; dice[5] = dice[1]; dice[1] = d; break; case 'W': d = dice[0]; dice[0] = dice[2]; dice[2] = dice[5]; dice[5] = dice[3]; dice[3] = d; break; case 'E': d = dice[0]; dice[0] = dice[3]; dice[3] = dice[5]; dice[5] = dice[2]; dice[2] = d; break; case 'N': d = dice[0]; dice[0] = dice[1]; dice[1] = dice[5]; dice[5] = dice[4]; dice[4] = d; break; case 'L': d = dice[1]; dice[1] = dice[3]; dice[3] = dice[4]; dice[4] = dice[2]; dice[2] = d; break; case 'R': d = dice[1]; dice[1] = dice[2]; dice[2] = dice[4]; dice[4] = dice[3]; dice[3] = d; break; } sum += dice[0]; } printf("%d\n", sum); scanf("%d", &n); } return 0; }
[["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 44]]
0
461
#include <stdio.h> int main() { int i, n, gou = 1; int f, v, t, r, q, s; int a[6]; char moji[10] = {'\0'}; while (1) { scanf("%d", &n); if (n == 0) break; a[0] = 1; a[1] = 2; a[2] = 6; a[3] = 5; a[4] = 4; a[5] = 3; for (i = 0; i < n; i++) { scanf("%s", moji); if (moji[0] == 'N') { f = a[0]; v = a[1]; t = a[2]; r = a[3]; q = a[4]; s = a[5]; a[0] = v; a[1] = t; a[2] = r; a[3] = f; a[4] = q; a[5] = s; gou += a[0]; } else if (moji[0] == 'E') { f = a[0]; v = a[1]; t = a[2]; r = a[3]; q = a[4]; s = a[5]; a[0] = q; a[1] = v; a[2] = s; a[3] = r; a[4] = t; a[5] = f; gou += a[0]; } else if (moji[0] == 'W') { f = a[0]; v = a[1]; t = a[2]; r = a[3]; q = a[4]; s = a[5]; a[0] = s; a[1] = v; a[2] = q; a[3] = r; a[4] = f; a[5] = t; gou += a[0]; } else if (moji[0] == 'S') { f = a[0]; v = a[1]; t = a[2]; r = a[3]; q = a[4]; s = a[5]; a[0] = r; a[1] = f; a[2] = v; a[3] = t; a[4] = q; a[5] = s; gou += a[0]; } else if (moji[0] == 'R') { f = a[0]; v = a[1]; t = a[2]; r = a[3]; q = a[4]; s = a[5]; a[0] = f; a[1] = s; a[2] = t; a[3] = q; a[4] = v; a[5] = r; gou += a[0]; } else if (moji[0] == 'L') { f = a[0]; v = a[1]; t = a[2]; r = a[3]; q = a[4]; s = a[5]; a[0] = f; a[1] = q; a[2] = t; a[3] = s; a[4] = r; a[5] = v; gou += a[0]; } } printf("%d\n", gou); } return 0; }
#include <stdio.h> int main() { int i, n, gou = 1; int f, v, t, r, q, s; int a[6]; char moji[10] = {'\0'}; while (1) { scanf("%d", &n); if (n == 0) break; a[0] = 1; a[1] = 2; a[2] = 6; a[3] = 5; a[4] = 4; a[5] = 3; gou = 1; for (i = 0; i < n; i++) { scanf("%s", moji); if (moji[0] == 'N') { f = a[0]; v = a[1]; t = a[2]; r = a[3]; q = a[4]; s = a[5]; a[0] = v; a[1] = t; a[2] = r; a[3] = f; a[4] = q; a[5] = s; gou += a[0]; } else if (moji[0] == 'E') { f = a[0]; v = a[1]; t = a[2]; r = a[3]; q = a[4]; s = a[5]; a[0] = q; a[1] = v; a[2] = s; a[3] = r; a[4] = t; a[5] = f; gou += a[0]; } else if (moji[0] == 'W') { f = a[0]; v = a[1]; t = a[2]; r = a[3]; q = a[4]; s = a[5]; a[0] = s; a[1] = v; a[2] = q; a[3] = r; a[4] = f; a[5] = t; gou += a[0]; } else if (moji[0] == 'S') { f = a[0]; v = a[1]; t = a[2]; r = a[3]; q = a[4]; s = a[5]; a[0] = r; a[1] = f; a[2] = v; a[3] = t; a[4] = q; a[5] = s; gou += a[0]; } else if (moji[0] == 'R') { f = a[0]; v = a[1]; t = a[2]; r = a[3]; q = a[4]; s = a[5]; a[0] = f; a[1] = s; a[2] = t; a[3] = q; a[4] = v; a[5] = r; gou += a[0]; } else if (moji[0] == 'L') { f = a[0]; v = a[1]; t = a[2]; r = a[3]; q = a[4]; s = a[5]; a[0] = f; a[1] = q; a[2] = t; a[3] = s; a[4] = r; a[5] = v; gou += a[0]; } } printf("%d\n", gou); } return 0; }
[["+", 0, 52, 8, 9, 0, 1, 0, 11, 31, 22], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 17, 32], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 12, 13], ["+", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35]]
0
780
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <iostream> #include <queue> #include <stack> #include <string> #include <vector> using namespace std; int main() { int n, dice[7] = {0, 1, 2, 3, 4, 5, 6}, sum = 1; char order[6]; while (1) { scanf("%d\n", &n); if (n == 0) break; sum = 1; for (int i = 0; i < 7; i++) dice[i] = i; for (int i = 0; i < n; i++) { scanf("%s", order); switch (order[0]) { case 'N': dice[0] = dice[1]; dice[1] = dice[2]; dice[2] = dice[6]; dice[6] = dice[5]; dice[5] = dice[0]; break; case 'E': dice[0] = dice[1]; dice[1] = dice[4]; dice[4] = dice[6]; dice[6] = dice[3]; dice[3] = dice[0]; break; case 'W': dice[0] = dice[1]; dice[1] = dice[3]; dice[3] = dice[6]; dice[6] = dice[4]; dice[4] = dice[0]; break; case 'S': dice[0] = dice[1]; dice[1] = dice[5]; dice[5] = dice[6]; dice[6] = dice[2]; dice[2] = dice[0]; break; case 'R': dice[0] = dice[2]; dice[2] = dice[3]; dice[3] = dice[5]; dice[5] = dice[4]; dice[4] = dice[0]; break; case 'L': dice[0] = dice[1]; dice[2] = dice[4]; dice[4] = dice[5]; dice[5] = dice[3]; dice[3] = dice[0]; break; } sum += dice[1]; } printf("%d\n", sum); } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <iostream> #include <queue> #include <stack> #include <string> #include <vector> using namespace std; int main() { int n, dice[7] = {0, 1, 2, 3, 4, 5, 6}, sum = 1; char order[6]; while (1) { scanf("%d", &n); if (n == 0) break; sum = 1; for (int i = 0; i < 7; i++) dice[i] = i; for (int i = 0; i < n; i++) { scanf("%s", order); switch (order[0]) { case 'N': dice[0] = dice[1]; dice[1] = dice[2]; dice[2] = dice[6]; dice[6] = dice[5]; dice[5] = dice[0]; break; case 'E': dice[0] = dice[1]; dice[1] = dice[4]; dice[4] = dice[6]; dice[6] = dice[3]; dice[3] = dice[0]; break; case 'W': dice[0] = dice[1]; dice[1] = dice[3]; dice[3] = dice[6]; dice[6] = dice[4]; dice[4] = dice[0]; break; case 'S': dice[0] = dice[1]; dice[1] = dice[5]; dice[5] = dice[6]; dice[6] = dice[2]; dice[2] = dice[0]; break; case 'R': dice[0] = dice[2]; dice[2] = dice[3]; dice[3] = dice[5]; dice[5] = dice[4]; dice[4] = dice[0]; break; case 'L': dice[0] = dice[2]; dice[2] = dice[4]; dice[4] = dice[5]; dice[5] = dice[3]; dice[3] = dice[0]; break; } sum += dice[1]; } printf("%d\n", sum); } return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 44], ["-", 0, 1, 0, 11, 12, 69, 341, 342, 0, 13], ["+", 0, 1, 0, 11, 12, 69, 341, 342, 0, 13]]
1
508
#include <iostream> #include <string> #include <vector> using namespace std; int main() { while (1) { int ans = 1, k, ue = 1, migi = 3, hidari = 2; cin >> k; if (k == 0) break; while (k--) { string a; int s, d; cin >> a; if (a == "North") { s = ue; ue = hidari; hidari = 7 - s; } else if (a == "East") { s = ue; ue = 7 - migi; migi = s; } else if (a == "West") { s = ue; ue = migi; migi = 7 - s; } else if (a == "South") { s = ue; ue = 7 - hidari; hidari = s; } else if (a == "Right") { s = migi; migi = hidari; hidari = 7 - s; } else if (a == "Left") { s = hidari; hidari = migi; migi = 7 - s; } ans += ue; } cout << ans << endl; } return 0; }
#include <iostream> #include <string> #include <vector> using namespace std; int main() { while (1) { int ans = 1, k, ue = 1, migi = 3, hidari = 2; cin >> k; if (k == 0) break; while (k--) { string a; int s, d; cin >> a; if (a == "North") { s = ue; ue = hidari; hidari = 7 - s; } else if (a == "East") { s = ue; ue = 7 - migi; migi = s; } else if (a == "West") { s = ue; ue = migi; migi = 7 - s; } else if (a == "South") { s = ue; ue = 7 - hidari; hidari = s; } else if (a == "Left") { s = migi; migi = hidari; hidari = 7 - s; } else if (a == "Right") { s = hidari; hidari = migi; migi = 7 - s; } ans += ue; } cout << ans << endl; } return 0; }
[["-", 0, 57, 15, 339, 51, 16, 12, 5, 0, 6], ["+", 0, 57, 15, 339, 51, 16, 12, 5, 0, 6]]
1
234
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; string list[] = {"North", "East", "South", "West", "Right", "Left"}; enum { UE, TEMAE, MIGI }; int main() { int n; while (cin >> n, n != 0) { int dice[3] = {1, 2, 3}; int score = 1; for (int i = 0; i < n; ++i) { string s; cin >> s; if (s == list[0]) { int ue_tmp; ue_tmp = dice[UE]; dice[UE] = dice[TEMAE]; dice[TEMAE] = 7 - ue_tmp; } if (s == list[1]) { int migi_tmp = dice[MIGI]; dice[MIGI] = dice[UE]; dice[UE] = 7 - migi_tmp; } if (s == list[2]) { int ue_tmp = dice[UE]; dice[UE] = dice[MIGI]; dice[MIGI] = 7 - ue_tmp; } if (s == list[3]) { int temae_tmp = dice[TEMAE]; dice[TEMAE] = dice[UE]; dice[UE] = 7 - temae_tmp; // 手前 => 上から // 上 => 手前のうら } if (s == list[4]) { int temae_tmp = dice[TEMAE]; dice[TEMAE] = dice[MIGI]; dice[MIGI] = 7 - temae_tmp; } if (s == list[5]) { int migi_tmp = dice[MIGI]; dice[MIGI] = dice[TEMAE]; dice[TEMAE] = 7 - migi_tmp; } score += dice[UE]; } cout << score << endl; } return 0; }
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; string list[] = {"North", "East", "West", "South", "Right", "Left"}; enum { UE, TEMAE, MIGI }; int main() { int n; while (cin >> n, n != 0) { int dice[3] = {1, 2, 3}; int score = 1; for (int i = 0; i < n; ++i) { string s; cin >> s; if (s == list[0]) { int ue_tmp; ue_tmp = dice[UE]; dice[UE] = dice[TEMAE]; dice[TEMAE] = 7 - ue_tmp; } if (s == list[1]) { int migi_tmp = dice[MIGI]; dice[MIGI] = dice[UE]; dice[UE] = 7 - migi_tmp; } if (s == list[2]) { int ue_tmp = dice[UE]; dice[UE] = dice[MIGI]; dice[MIGI] = 7 - ue_tmp; } if (s == list[3]) { int temae_tmp = dice[TEMAE]; dice[TEMAE] = dice[UE]; dice[UE] = 7 - temae_tmp; // 手前 => 上から // 上 => 手前のうら } if (s == list[4]) { int temae_tmp = dice[TEMAE]; dice[TEMAE] = dice[MIGI]; dice[MIGI] = 7 - temae_tmp; } if (s == list[5]) { int migi_tmp = dice[MIGI]; dice[MIGI] = dice[TEMAE]; dice[TEMAE] = 7 - migi_tmp; } score += dice[UE]; } cout << score << endl; } return 0; }
[["-", 0, 43, 49, 50, 51, 83, 0, 5, 0, 6], ["+", 0, 43, 49, 50, 51, 83, 0, 5, 0, 6]]
1
363
#include <iostream> using namespace std; int main() { int t, n, sam; string comand; while (cin >> n, n != 0) { sam = 0; int dice[6] = {1, 2, 3, 5, 4, 6}; for (int i = 0; i < n; i++) { cin >> comand; if (comand == "North") { t = dice[0]; dice[0] = dice[1]; dice[1] = dice[5]; dice[5] = dice[3]; dice[3] = t; } else if (comand == "East") { t = dice[0]; dice[0] = dice[4]; dice[4] = dice[5]; dice[5] = dice[2]; dice[2] = t; } else if (comand == "West") { t = dice[0]; dice[0] = dice[2]; dice[2] = dice[5]; dice[5] = dice[4]; dice[4] = t; } else if (comand == "South") { t = dice[0]; dice[0] = dice[3]; dice[3] = dice[5]; dice[5] = dice[1]; dice[1] = t; } else if (comand == "Right") { t = dice[1]; dice[1] = dice[2]; dice[2] = dice[3]; dice[3] = dice[4]; dice[4] = t; } else if (comand == "Left") { t = dice[1]; dice[1] = dice[4]; dice[4] = dice[3]; dice[3] = dice[2]; dice[2] = t; } sam += dice[0]; } cout << sam << endl; } return 0; }
#include <iostream> using namespace std; int main() { int t, n, sam; string comand; while (cin >> n, n != 0) { sam = 1; int dice[6] = {1, 2, 3, 5, 4, 6}; for (int i = 0; i < n; i++) { cin >> comand; if (comand == "North") { t = dice[0]; dice[0] = dice[1]; dice[1] = dice[5]; dice[5] = dice[3]; dice[3] = t; } else if (comand == "East") { t = dice[0]; dice[0] = dice[4]; dice[4] = dice[5]; dice[5] = dice[2]; dice[2] = t; } else if (comand == "West") { t = dice[0]; dice[0] = dice[2]; dice[2] = dice[5]; dice[5] = dice[4]; dice[4] = t; } else if (comand == "South") { t = dice[0]; dice[0] = dice[3]; dice[3] = dice[5]; dice[5] = dice[1]; dice[1] = t; } else if (comand == "Right") { t = dice[1]; dice[1] = dice[2]; dice[2] = dice[3]; dice[3] = dice[4]; dice[4] = t; } else if (comand == "Left") { t = dice[1]; dice[1] = dice[4]; dice[4] = dice[3]; dice[3] = dice[2]; dice[2] = t; } sam += dice[0]; } cout << sam << endl; } return 0; }
[["-", 0, 52, 8, 9, 0, 1, 0, 11, 12, 13], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 12, 13]]
1
423
#include <algorithm> #include <cmath> #include <iostream> #include <string> using namespace std; int dice[6], kari[6], sum, n; string str; int main() { while (1) { for (int i = 0; i < 6; i++) { dice[i] = i + 1; kari[i] = dice[i]; } sum += dice[0]; cin >> n; if (n == 0) { break; } for (int i = 0; i < n; i++) { cin >> str; if (str == "North") { dice[4] = kari[0]; dice[5] = kari[4]; dice[1] = kari[5]; dice[0] = kari[1]; } else if (str == "East") { dice[2] = kari[0]; dice[5] = kari[2]; dice[3] = kari[5]; dice[0] = kari[3]; } else if (str == "West") { dice[3] = kari[0]; dice[5] = kari[3]; dice[2] = kari[5]; dice[0] = kari[2]; } else if (str == "South") { dice[1] = kari[0]; dice[5] = kari[1]; dice[4] = kari[5]; dice[0] = kari[4]; } else if (str == "Right") { dice[1] = kari[2]; dice[3] = kari[1]; dice[4] = kari[3]; dice[2] = kari[4]; } else if (str == "Left") { dice[2] = kari[1]; dice[4] = kari[2]; dice[3] = kari[4]; dice[1] = kari[3]; } for (int i = 0; i < 6; i++) { kari[i] = dice[i]; } sum += dice[0]; } cout << sum << endl; } return 0; }
#include <algorithm> #include <cmath> #include <iostream> #include <string> using namespace std; int dice[6], kari[6], sum, n; string str; int main() { while (1) { sum = 0; for (int i = 0; i < 6; i++) { dice[i] = i + 1; kari[i] = dice[i]; } sum += dice[0]; cin >> n; if (n == 0) { break; } for (int i = 0; i < n; i++) { cin >> str; if (str == "North") { dice[4] = kari[0]; dice[5] = kari[4]; dice[1] = kari[5]; dice[0] = kari[1]; } else if (str == "East") { dice[2] = kari[0]; dice[5] = kari[2]; dice[3] = kari[5]; dice[0] = kari[3]; } else if (str == "West") { dice[3] = kari[0]; dice[5] = kari[3]; dice[2] = kari[5]; dice[0] = kari[2]; } else if (str == "South") { dice[1] = kari[0]; dice[5] = kari[1]; dice[4] = kari[5]; dice[0] = kari[4]; } else if (str == "Right") { dice[1] = kari[2]; dice[3] = kari[1]; dice[4] = kari[3]; dice[2] = kari[4]; } else if (str == "Left") { dice[2] = kari[1]; dice[4] = kari[2]; dice[3] = kari[4]; dice[1] = kari[3]; } for (int i = 0; i < 6; i++) { kari[i] = dice[i]; } sum += dice[0]; } cout << sum << endl; } return 0; }
[["+", 0, 52, 8, 9, 0, 1, 0, 11, 31, 22], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 17, 32], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 12, 13], ["+", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35]]
1
465
#include <bits/stdc++.h> using namespace std; #define FOR(i, k, n) for (int i = (k); i < (n); i++) #define REP(i, n) FOR(i, 0, n) #define ALL(a) a.begin(), a.end() #define MS(m, v) memset(m, v, sizeof(m)) #define D10 fixed << setprecision(10) typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef vector<string> vs; typedef pair<int, int> pii; const int MOD = 1000000007; const int INF = MOD + 1; const ld EPS = 1e-12; template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); } template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); } /*--------------------template--------------------*/ struct UnionFind { vector<int> par; UnionFind(int n) : par(n, -1) {} int find(int x) { return par[x] < 0 ? x : par[x] = find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (par[y] < par[x]) swap(x, y); if (par[x] == par[y]) par[x]--; par[y] = x; if (find(x) != find(y)) par[find(y)] = x; } int count() { int cnt = 0; REP(i, par.size()) if (par[i] < 0) cnt++; return cnt; } bool same(int x, int y) { return find(x) == find(y); } }; struct Dice { private: int top, right, front; public: Dice() : top(1), right(2), front(3){}; Dice(int t, int r, int f) : top(t), right(r), front(f){}; void set(int t, int r, int f) { top = t, right = r, front = f; } int get_top() { return top; } int get_bottom() { return 7 - top; } int get_right() { return right; } int get_left() { return 7 - right; } int get_front() { return front; } int get_back() { return 7 - front; } void to_front() { swap(front, top); top = 7 - top; } void to_back() { REP(i, 3) to_front(); } void to_right() { swap(top, right); top = 7 - top; } void to_left() { REP(i, 3) to_right(); } void to_clock() { swap(front, right); right = 7 - right; } void to_counter() { REP(i, 3) to_clock(); } }; int main() { int n; while (cin >> n, n) { Dice d(1, 3, 2); int ans = 0; REP(i, n) { string s; cin >> s; if (s == "North") d.to_back(); else if (s == "South") d.to_front(); else if (s == "East") d.to_right(); else if (s == "West") d.to_left(); else if (s == "Right") d.to_clock(); else if (s == "Left") d.to_counter(); ans += d.get_top(); } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define FOR(i, k, n) for (int i = (k); i < (n); i++) #define REP(i, n) FOR(i, 0, n) #define ALL(a) a.begin(), a.end() #define MS(m, v) memset(m, v, sizeof(m)) #define D10 fixed << setprecision(10) typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef vector<string> vs; typedef pair<int, int> pii; const int MOD = 1000000007; const int INF = MOD + 1; const ld EPS = 1e-12; template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); } template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); } /*--------------------template--------------------*/ struct UnionFind { vector<int> par; UnionFind(int n) : par(n, -1) {} int find(int x) { return par[x] < 0 ? x : par[x] = find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (par[y] < par[x]) swap(x, y); if (par[x] == par[y]) par[x]--; par[y] = x; if (find(x) != find(y)) par[find(y)] = x; } int count() { int cnt = 0; REP(i, par.size()) if (par[i] < 0) cnt++; return cnt; } bool same(int x, int y) { return find(x) == find(y); } }; struct Dice { private: int top, right, front; public: Dice() : top(1), right(2), front(3){}; Dice(int t, int r, int f) : top(t), right(r), front(f){}; void set(int t, int r, int f) { top = t, right = r, front = f; } int get_top() { return top; } int get_bottom() { return 7 - top; } int get_right() { return right; } int get_left() { return 7 - right; } int get_front() { return front; } int get_back() { return 7 - front; } void to_front() { swap(front, top); top = 7 - top; } void to_back() { REP(i, 3) to_front(); } void to_right() { swap(top, right); top = 7 - top; } void to_left() { REP(i, 3) to_right(); } void to_clock() { swap(front, right); right = 7 - right; } void to_counter() { REP(i, 3) to_clock(); } }; int main() { int n; while (cin >> n, n) { Dice d(1, 3, 2); int ans = 1; REP(i, n) { string s; cin >> s; if (s == "North") d.to_back(); else if (s == "South") d.to_front(); else if (s == "East") d.to_right(); else if (s == "West") d.to_left(); else if (s == "Right") d.to_clock(); else if (s == "Left") d.to_counter(); ans += d.get_top(); } cout << ans << endl; } return 0; }
[["-", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13]]
1
781
#include <bits/stdc++.h> #define range(i, a, b) for (int i = (a); i < (b); i++) #define rep(i, b) range(i, 0, b) #define pb(a) push_back(a) #define all(a) (a).begin(), (a).end() #define debug(x) cout << "debug " << x << endl; using namespace std; void order(int d[6], string str) { int temp; if (str == "North") { temp = d[2]; d[2] = d[4]; d[4] = d[5]; d[5] = d[0]; d[0] = temp; } else if (str == "East") { temp = d[2]; d[2] = d[1]; d[1] = d[5]; d[5] = d[3]; d[3] = temp; } else if (str == "West") { temp = d[2]; d[2] = d[3]; d[3] = d[5]; d[5] = d[1]; d[1] = temp; } else if (str == "South") { temp = d[2]; d[2] = d[0]; d[0] = d[5]; d[5] = d[4]; d[4] = temp; } else if (str == "Right") { temp = d[0]; d[0] = d[1]; d[1] = d[4]; d[4] = d[3]; d[3] = temp; } else { temp = d[0]; d[0] = d[3]; d[3] = d[4]; d[4] = d[1]; d[1] = temp; } } int main() { int n; while (cin >> n) { int d[6] = {5, 4, 1, 3, 2, 6}, sum = 0; rep(i, n) { string str; cin >> str; order(d, str); sum += d[2]; } cout << sum + 1 << endl; } }
#include <bits/stdc++.h> #define range(i, a, b) for (int i = (a); i < (b); i++) #define rep(i, b) range(i, 0, b) #define pb(a) push_back(a) #define all(a) (a).begin(), (a).end() #define debug(x) cout << "debug " << x << endl; using namespace std; void order(int d[6], string str) { int temp; if (str == "North") { temp = d[2]; d[2] = d[4]; d[4] = d[5]; d[5] = d[0]; d[0] = temp; } else if (str == "East") { temp = d[2]; d[2] = d[1]; d[1] = d[5]; d[5] = d[3]; d[3] = temp; } else if (str == "West") { temp = d[2]; d[2] = d[3]; d[3] = d[5]; d[5] = d[1]; d[1] = temp; } else if (str == "South") { temp = d[2]; d[2] = d[0]; d[0] = d[5]; d[5] = d[4]; d[4] = temp; } else if (str == "Right") { temp = d[0]; d[0] = d[1]; d[1] = d[4]; d[4] = d[3]; d[3] = temp; } else { temp = d[0]; d[0] = d[3]; d[3] = d[4]; d[4] = d[1]; d[1] = temp; } } int main() { int n; while (cin >> n) { int d[6] = {5, 4, 1, 3, 2, 6}, sum = 0; rep(i, n) { string str; cin >> str; order(d, str); sum += d[2]; } if (n) cout << sum + 1 << endl; } }
[["+", 8, 9, 0, 52, 8, 9, 0, 57, 0, 121], ["+", 0, 52, 8, 9, 0, 57, 15, 339, 0, 24], ["+", 0, 52, 8, 9, 0, 57, 15, 339, 51, 22], ["+", 0, 52, 8, 9, 0, 57, 15, 339, 0, 25]]
1
458
#include <iostream> #include <string> #include <vector> class Sai { public: int up; int down; int north; int east; int west; int south; Sai() { up = 1; down = 6; south = 2; east = 3; north = 5; west = 4; } void North() { int temp = up; up = south; south = down; down = north; north = temp; } void East() { int temp = up; up = west; west = down; down = east; east = temp; } void West() { int temp = up; up = east; east = down; down = west; west = temp; } void South() { int temp = up; up = north; north = down; down = south; south = temp; } void Right() { int temp = south; south = east; east = north; north = west; west = temp; } void Left() { int temp = south; south = west; west = north; north = east; east = temp; } }; int main() { int n; std::string str; std::vector<int> ans; Sai sai; while (1) { std::cin >> n; if (n == 0) { break; } int answer = 1; for (int i = 0; i < n; i++) { std::cin >> str; if (str == "North") { sai.North(); answer += sai.up; } else if (str == "East") { sai.East(); answer += sai.up; } else if (str == "West") { sai.West(); answer += sai.up; } else if (str == "South") { sai.South(); answer += sai.up; } else if (str == "Right") { sai.Right(); answer += sai.up; } else if (str == "Left") { sai.Left(); answer += sai.up; } } ans.push_back(answer); } for (unsigned int i = 0; i < ans.size(); i++) { std::cout << ans[i] << std::endl; } return 0; }
#include <iostream> #include <string> #include <vector> class Sai { public: int up; int down; int north; int east; int west; int south; Sai() { up = 1; down = 6; south = 2; east = 3; north = 5; west = 4; } void North() { int temp = up; up = south; south = down; down = north; north = temp; } void East() { int temp = up; up = west; west = down; down = east; east = temp; } void West() { int temp = up; up = east; east = down; down = west; west = temp; } void South() { int temp = up; up = north; north = down; down = south; south = temp; } void Right() { int temp = south; south = east; east = north; north = west; west = temp; } void Left() { int temp = south; south = west; west = north; north = east; east = temp; } }; int main() { int n; std::string str; std::vector<int> ans; while (1) { std::cin >> n; if (n == 0) { break; } int answer = 1; Sai sai; for (int i = 0; i < n; i++) { std::cin >> str; if (str == "North") { sai.North(); answer += sai.up; } else if (str == "East") { sai.East(); answer += sai.up; } else if (str == "West") { sai.West(); answer += sai.up; } else if (str == "South") { sai.South(); answer += sai.up; } else if (str == "Right") { sai.Right(); answer += sai.up; } else if (str == "Left") { sai.Left(); answer += sai.up; } } ans.push_back(answer); } for (unsigned int i = 0; i < ans.size(); i++) { std::cout << ans[i] << std::endl; } return 0; }
[["-", 0, 30, 0, 14, 8, 9, 0, 43, 39, 78], ["-", 0, 30, 0, 14, 8, 9, 0, 43, 49, 22], ["-", 0, 30, 0, 14, 8, 9, 0, 43, 0, 35], ["+", 8, 9, 0, 52, 8, 9, 0, 43, 0, 35], ["+", 8, 9, 0, 52, 8, 9, 0, 43, 39, 78], ["+", 8, 9, 0, 52, 8, 9, 0, 43, 49, 22]]
1
477
#include "bits/stdc++.h" #include <unordered_map> #include <unordered_set> #pragma warning(disable : 4996) using namespace std; using ld = long double; template <class t> using table = vector<vector<t>>; const ld eps = 1e-9; //< "d:\d_download\visual studio //2015\projects\programing_contest_c++\debug\a.txt" > "d:\d_download\visual //studio 2015\projects\programing_contest_c++\debug\b.answer" enum R_Way { R_Left, R_Front, R_Right, R_Back, R_Dummy, }; struct Dice { int tp; int bo; int no; int so; int ea; int we; Dice(int tp_, int bo_, int no_, int so_, int ea_, int we_) : tp(tp_), bo(bo_), no(no_), so(so_), ea(ea_), we(we_) {} Dice(vector<int> pips_) {} Dice() { tp = 1; /*bo = 6; no = 2; so = 5; ea = 3; we = 4;*/ } int rotate(const R_Way way) { switch (way) { case R_Right: { int c = tp; tp = we; we = bo; bo = ea; ea = c; } break; case R_Back: { int c = tp; tp = so; so = bo; bo = no; no = c; } break; case R_Left: { int c = tp; tp = ea; ea = bo; bo = we; we = c; } break; case R_Front: { int c = tp; tp = no; no = bo; bo = so; so = c; } break; default: assert(false); } return tp; } int rotate(const int way) { return rotate(static_cast<R_Way>(way)); } void spin(const int left) { if (left) { rotate(R_Way::R_Left); rotate(R_Way::R_Front); rotate(R_Way::R_Right); } else { rotate(R_Way::R_Left); rotate(R_Way::R_Front); rotate(R_Way::R_Right); } } int hash() const { return tp + we * 6 + no * 36; } }; int main() { while (1) { int N; cin >> N; if (!N) break; map<string, int> mp; mp["West"] = 0; mp["South"] = 1; mp["East"] = 2; mp["North"] = 3; mp["Right"] = 4; mp["Left"] = 5; Dice di(1, 6, 5, 2, 3, 4); int ans = 0; while (N--) { string st; cin >> st; int type = mp[st]; if (type < 4) { di.rotate(type); } else { di.spin(type - 4); } ans += di.tp; } cout << ans + 1 << endl; } return 0; }
#include "bits/stdc++.h" #include <unordered_map> #include <unordered_set> #pragma warning(disable : 4996) using namespace std; using ld = long double; template <class t> using table = vector<vector<t>>; const ld eps = 1e-9; //< "d:\d_download\visual studio //2015\projects\programing_contest_c++\debug\a.txt" > "d:\d_download\visual //studio 2015\projects\programing_contest_c++\debug\b.answer" enum R_Way { R_Left, R_Front, R_Right, R_Back, R_Dummy, }; struct Dice { int tp; int bo; int no; int so; int ea; int we; Dice(int tp_, int bo_, int no_, int so_, int ea_, int we_) : tp(tp_), bo(bo_), no(no_), so(so_), ea(ea_), we(we_) {} Dice(vector<int> pips_) {} Dice() { tp = 1; /*bo = 6; no = 2; so = 5; ea = 3; we = 4;*/ } int rotate(const R_Way way) { switch (way) { case R_Right: { int c = tp; tp = we; we = bo; bo = ea; ea = c; } break; case R_Back: { int c = tp; tp = so; so = bo; bo = no; no = c; } break; case R_Left: { int c = tp; tp = ea; ea = bo; bo = we; we = c; } break; case R_Front: { int c = tp; tp = no; no = bo; bo = so; so = c; } break; default: assert(false); } return tp; } int rotate(const int way) { return rotate(static_cast<R_Way>(way)); } void spin(const int left) { if (left) { rotate(R_Way::R_Left); rotate(R_Way::R_Back); rotate(R_Way::R_Right); } else { rotate(R_Way::R_Left); rotate(R_Way::R_Front); rotate(R_Way::R_Right); } } int hash() const { return tp + we * 6 + no * 36; } }; int main() { while (1) { int N; cin >> N; if (!N) break; map<string, int> mp; mp["West"] = 0; mp["South"] = 1; mp["East"] = 2; mp["North"] = 3; mp["Right"] = 4; mp["Left"] = 5; Dice di(1, 6, 5, 2, 3, 4); int ans = 0; while (N--) { string st; cin >> st; int type = mp[st]; if (type < 4) { di.rotate(type); } else { di.spin((type - 4)); } ans += di.tp; } cout << ans + 1 << endl; } return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 343, 141, 22], ["+", 0, 1, 0, 2, 3, 4, 0, 343, 141, 22], ["+", 0, 9, 0, 1, 0, 2, 3, 4, 0, 24], ["+", 0, 1, 0, 2, 3, 4, 0, 23, 0, 25]]
1
566
#include <algorithm> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define REP(i, n) for (int(i) = 0; (i) < (int)(n); (i)++) #define RREP(i, n) for (int(i) = (int)(n)-1; (i) >= 0; (i)--) #define REMOVE(Itr, n) \ (Itr).erase(remove((Itr).begin(), (Itr).end(), n), (Itr).end()) #define PB_VEC(Itr1, Itr2) \ (Itr1).insert((Itr1).end(), (Itr2).begin(), (Itr2).end()) #define UNIQUE(Itr) \ sort((Itr).begin(), (Itr).end()); \ (Itr).erase(unique((Itr).begin(), (Itr).end()), (Itr).end()) typedef long long ll; int main() { while (true) { int n; cin >> n; if (n == 0) break; int dice[4] = {5, 3, 2, 4}; int up = 1; int ans = 1; REP(i, n) { string dire; cin >> dire; if (dire == "North") { dice[0] = up; up = dice[2]; dice[2] = 7 - dice[0]; } else if (dire == "East") { dice[1] = up; up = dice[3]; dice[3] = 7 - dice[1]; } else if (dire == "South") { dice[2] = up; up = dice[0]; dice[0] = 7 - dice[2]; } else if (dire == "West") { dice[3] = up; up = dice[1]; dice[1] = 7 - dice[3]; } else if (dire == "Right") { int t = dice[0]; dice[0] = dice[3]; dice[3] = dice[2]; dice[2] = dice[1]; dice[1] = t; } else { int t = dice[0]; dice[0] = dice[1]; dice[1] = dice[2]; dice[2] = dice[3]; dice[3] = t; } } cout << ans << endl; } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define REP(i, n) for (int(i) = 0; (i) < (int)(n); (i)++) #define RREP(i, n) for (int(i) = (int)(n)-1; (i) >= 0; (i)--) #define REMOVE(Itr, n) \ (Itr).erase(remove((Itr).begin(), (Itr).end(), n), (Itr).end()) #define PB_VEC(Itr1, Itr2) \ (Itr1).insert((Itr1).end(), (Itr2).begin(), (Itr2).end()) #define UNIQUE(Itr) \ sort((Itr).begin(), (Itr).end()); \ (Itr).erase(unique((Itr).begin(), (Itr).end()), (Itr).end()) typedef long long ll; int main() { while (true) { int n; cin >> n; if (n == 0) break; int dice[4] = {5, 3, 2, 4}; int up = 1; int ans = 1; REP(i, n) { string dire; cin >> dire; if (dire == "North") { dice[0] = up; up = dice[2]; dice[2] = 7 - dice[0]; } else if (dire == "East") { dice[1] = up; up = dice[3]; dice[3] = 7 - dice[1]; } else if (dire == "South") { dice[2] = up; up = dice[0]; dice[0] = 7 - dice[2]; } else if (dire == "West") { dice[3] = up; up = dice[1]; dice[1] = 7 - dice[3]; } else if (dire == "Right") { int t = dice[0]; dice[0] = dice[3]; dice[3] = dice[2]; dice[2] = dice[1]; dice[1] = t; } else { int t = dice[0]; dice[0] = dice[1]; dice[1] = dice[2]; dice[2] = dice[3]; dice[3] = t; } ans += up; } cout << ans << endl; } return 0; }
[["+", 0, 14, 8, 9, 0, 1, 0, 11, 31, 22], ["+", 0, 14, 8, 9, 0, 1, 0, 11, 17, 107], ["+", 0, 14, 8, 9, 0, 1, 0, 11, 12, 22], ["+", 8, 9, 0, 14, 8, 9, 0, 1, 0, 35]]
1
402
#include <algorithm> #include <cctype> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <memory> #include <numeric> #include <queue> #include <string> #include <utility> #include <vector> #define REP(i, n) for (int i = 0; i < n; i++) #define REPR(i, n) for (int i = n; i >= 0; i--) #define FOR(i, m, n) for (int i = m; i < n; i++) #define FORR(i, m, n) for (int i = m; i >= n; i--) #define SORT(v, n) sort(v, v + n); #define VSORT(v) sort(v.begin(), v.end()); #define pb(a) push_back(a) #define INF 999999999 using namespace std; typedef vector<int> vi; typedef vector<long long> vl; typedef vector<double> vd; typedef pair<int, int> pii; typedef pair<long, long> pll; typedef long long ll; int dy[] = {0, 0, 1, -1, 0}; int dx[] = {1, -1, 0, 0, 0}; int main() { ios::sync_with_stdio(false); int n; while (1) { scanf("%d", &n); if (n == 0) break; int Dice[3] = {1, 2, 3}; char str[10]; int sum = 1; while (n--) { scanf("%s", str); int temp = Dice[0]; switch (str[0]) { case 'N': { Dice[0] = Dice[1]; Dice[1] = 7 - temp; break; } case 'S': { Dice[0] = 7 - Dice[1]; Dice[1] = temp; break; } case 'E': { Dice[0] = 7 - Dice[2]; Dice[2] = temp; break; } case 'W': { Dice[0] = Dice[2]; Dice[2] = 7 - temp; break; } case 'L': { temp = Dice[1]; Dice[1] = Dice[2]; Dice[2] = 7 - temp; break; } case 'R': { temp = Dice[1]; Dice[1] = 7 - Dice[2]; Dice[2] = temp; break; } default: break; } sum += Dice[0]; } printf("%d\n", sum); } }
#include <algorithm> #include <cctype> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <memory> #include <numeric> #include <queue> #include <string> #include <utility> #include <vector> #define REP(i, n) for (int i = 0; i < n; i++) #define REPR(i, n) for (int i = n; i >= 0; i--) #define FOR(i, m, n) for (int i = m; i < n; i++) #define FORR(i, m, n) for (int i = m; i >= n; i--) #define SORT(v, n) sort(v, v + n); #define VSORT(v) sort(v.begin(), v.end()); #define pb(a) push_back(a) #define INF 999999999 using namespace std; typedef vector<int> vi; typedef vector<long long> vl; typedef vector<double> vd; typedef pair<int, int> pii; typedef pair<long, long> pll; typedef long long ll; int dy[] = {0, 0, 1, -1, 0}; int dx[] = {1, -1, 0, 0, 0}; int main() { ios::sync_with_stdio(false); int n; while (1) { scanf("%d", &n); if (n == 0) break; int Dice[3] = {1, 2, 3}; char str[10]; int sum = 1; while (n--) { scanf("%s", str); int temp = Dice[0]; switch (str[0]) { case 'N': { Dice[0] = Dice[1]; Dice[1] = 7 - temp; break; } case 'S': { Dice[0] = 7 - Dice[1]; Dice[1] = temp; break; } case 'E': { Dice[0] = 7 - Dice[2]; Dice[2] = temp; break; } case 'W': { Dice[0] = Dice[2]; Dice[2] = 7 - temp; break; } case 'R': { temp = Dice[1]; Dice[1] = Dice[2]; Dice[2] = 7 - temp; break; } case 'L': { temp = Dice[1]; Dice[1] = 7 - Dice[2]; Dice[2] = temp; break; } default: break; } sum += Dice[0]; } printf("%d\n", sum); } }
[["-", 0, 99, 8, 9, 0, 100, 51, 103, 0, 125], ["+", 0, 99, 8, 9, 0, 100, 51, 103, 0, 125]]
1
483
#include <algorithm> #include <functional> #include <iostream> #include <map> #include <string> #include <utility> #include <vector> using namespace std; int main() { while (true) { int n; cin >> n; if (n == 0) { break; } int ue = 1, sita = 6, temae = 2, oku = 5, migi = 3, hidari = 4; int score = 0; for (int i = 0; i < n; ++i) { int keep = 0; string move; cin >> move; if (move == "South") { keep = ue; ue = oku; oku = sita; sita = temae; temae = keep; } else if (move == "North") { keep = ue; ue = temae; temae = sita; sita = oku; oku = keep; } else if (move == "East") { keep = ue; ue = hidari; hidari = sita; sita = migi; migi = keep; } else if (move == "West") { keep = ue; ue = migi; migi = sita; sita = hidari; hidari = keep; } else if (move == "Right") { keep = temae; temae = migi; migi = oku; oku = hidari; hidari = keep; } else { keep = temae; temae = hidari; hidari = oku; oku = migi; migi = keep; } score += ue; } cout << score << endl; } return 0; }
#include <algorithm> #include <functional> #include <iostream> #include <map> #include <string> #include <utility> #include <vector> using namespace std; int main() { while (true) { int n; cin >> n; if (n == 0) { break; } int ue = 1, sita = 6, temae = 2, oku = 5, migi = 3, hidari = 4; int score = 1; for (int i = 0; i < n; ++i) { int keep = 0; string move; cin >> move; if (move == "South") { keep = ue; ue = oku; oku = sita; sita = temae; temae = keep; } else if (move == "North") { keep = ue; ue = temae; temae = sita; sita = oku; oku = keep; } else if (move == "East") { keep = ue; ue = hidari; hidari = sita; sita = migi; migi = keep; } else if (move == "West") { keep = ue; ue = migi; migi = sita; sita = hidari; hidari = keep; } else if (move == "Right") { keep = temae; temae = migi; migi = oku; oku = hidari; hidari = keep; } else { keep = temae; temae = hidari; hidari = oku; oku = migi; migi = keep; } score += ue; } cout << score << endl; } return 0; }
[["-", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13]]
1
295
#include <algorithm> #include <iostream> #include <math.h> #include <stdio.h> #include <string.h> using namespace std; int main() { int n = 5, s = 2, e = 3, w = 4, t = 1, b = 6; int y, z; int p = 1; char sn[10]; while (1) { cin >> y; if (y == 0) { break; } for (int i = 0; i < y; i++) { cin >> sn; if (sn[0] == 'N') { z = t; t = s; s = b; b = n; n = z; p = p + t; } else if (sn[0] == 'S') { z = t; t = n; n = b; b = s; s = z; p = p + t; } else if (sn[0] == 'E') { z = t; t = w; w = b; b = e; e = z; p = p + t; } else if (sn[0] == 'W') { z = t; t = e; e = b; b = w; w = z; p = p + t; } else if (sn[0] == 'L') { z = s; s = e; e = n; n = w; w = z; p = p + t; } else if (sn[0] == 'R') { z = s; s = w; w = n; n = e; s = z; p = p + t; } } cout << p << endl; n = 5, s = 2, e = 3, w = 4, t = 1, b = 6; p = 1; } return 0; }
#include <algorithm> #include <iostream> #include <math.h> #include <stdio.h> #include <string.h> using namespace std; int main() { int n = 5, s = 2, e = 3, w = 4, t = 1, b = 6; int y, z; int p = 1; char sn[10]; while (1) { cin >> y; if (y == 0) { break; } for (int i = 0; i < y; i++) { cin >> sn; if (sn[0] == 'N') { z = t; t = s; s = b; b = n; n = z; p = p + t; } else if (sn[0] == 'S') { z = t; t = n; n = b; b = s; s = z; p = p + t; } else if (sn[0] == 'E') { z = t; t = w; w = b; b = e; e = z; p = p + t; } else if (sn[0] == 'W') { z = t; t = e; e = b; b = w; w = z; p = p + t; } else if (sn[0] == 'R') { z = s; s = e; e = n; n = w; w = z; p = p + t; } else if (sn[0] == 'L') { z = s; s = w; w = n; n = e; e = z; p = p + t; } } cout << p << endl; n = 5, s = 2, e = 3, w = 4, t = 1, b = 6; p = 1; } return 0; }
[["-", 0, 57, 15, 339, 51, 16, 12, 103, 0, 125], ["+", 0, 57, 15, 339, 51, 16, 12, 103, 0, 125], ["-", 0, 57, 64, 9, 0, 1, 0, 11, 31, 22], ["+", 0, 57, 64, 9, 0, 1, 0, 11, 31, 22]]
1
377
// dice #include <stdio.h> int dice[6]; void swap(int a, int b, int c, int d) { int temp; temp = dice[a]; dice[a] = dice[b]; dice[b] = dice[c]; dice[c] = dice[d]; dice[d] = temp; } int main(void) { int n, i, r; char s[8]; while (1) { scanf("%d", &n); if (n == 0) { break; } r = 0; dice[0] = 1; dice[1] = 2; dice[2] = 3; dice[3] = 5; dice[4] = 4; dice[5] = 6; for (i = 0; i < n; i++) { scanf("%s", s); if (s[0] == 'N') { swap(0, 1, 5, 3); } else if (s[0] == 'E') { swap(0, 4, 5, 2); } else if (s[0] == 'W') { swap(0, 2, 5, 4); } else if (s[0] == 'S') { swap(0, 3, 5, 1); } else if (s[0] == 'R') { swap(1, 2, 3, 4); } else { swap(1, 4, 3, 2); } r += dice[0]; } printf("%d\n", r); } return 0; }
// dice #include <stdio.h> int dice[6]; void swap(int a, int b, int c, int d) { int temp; temp = dice[a]; dice[a] = dice[b]; dice[b] = dice[c]; dice[c] = dice[d]; dice[d] = temp; } int main(void) { int n, i, r; char s[8]; while (1) { scanf("%d", &n); if (n == 0) { break; } r = 1; dice[0] = 1; dice[1] = 2; dice[2] = 3; dice[3] = 5; dice[4] = 4; dice[5] = 6; for (i = 0; i < n; i++) { scanf("%s", s); if (s[0] == 'N') { swap(0, 1, 5, 3); } else if (s[0] == 'E') { swap(0, 4, 5, 2); } else if (s[0] == 'W') { swap(0, 2, 5, 4); } else if (s[0] == 'S') { swap(0, 3, 5, 1); } else if (s[0] == 'R') { swap(1, 2, 3, 4); } else { swap(1, 4, 3, 2); } r += dice[0]; } printf("%d\n", r); } return 0; }
[["-", 0, 52, 8, 9, 0, 1, 0, 11, 12, 13], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 12, 13]]
1
347
#include <stdio.h> int main() { int dice[] = {1, 2, 3}, aa, x, t; char a[10]; while (1) { dice[0] = 1; dice[1] = 2; dice[2] = 3; scanf("%d", &aa); if (aa == 0) break; t = 1; for (int i = 0; i < aa; i++) { scanf("%s", &a); if (a[0] == 'N') { x = dice[0]; dice[0] = dice[1]; dice[1] = 7 - x; } if (a[0] == 'S') { x = dice[0]; dice[0] = 7 - dice[1]; dice[1] = x; } if (a[0] == 'E') { x = dice[0]; dice[0] = 7 - dice[2]; dice[2] = x; } if (a[0] == 'W') { x = dice[0]; dice[0] = dice[2]; dice[2] = 7 - x; } if (a[0] == 'L') { x = dice[1]; dice[1] = 7 - dice[2]; dice[2] = x; } if (a[0] == 'R') { x = dice[1]; dice[1] = dice[2]; dice[2] = 7 - x; } t = t + dice[0]; } printf("%d", t); } return 0; }
#include <stdio.h> int main() { int dice[] = {1, 2, 3}, aa, x, t; char a[10]; while (1) { dice[0] = 1; dice[1] = 2; dice[2] = 3; scanf("%d", &aa); if (aa == 0) break; t = 1; for (int i = 0; i < aa; i++) { scanf("%s", &a); if (a[0] == 'N') { x = dice[0]; dice[0] = dice[1]; dice[1] = 7 - x; } if (a[0] == 'S') { x = dice[0]; dice[0] = 7 - dice[1]; dice[1] = x; } if (a[0] == 'E') { x = dice[0]; dice[0] = 7 - dice[2]; dice[2] = x; } if (a[0] == 'W') { x = dice[0]; dice[0] = dice[2]; dice[2] = 7 - x; } if (a[0] == 'L') { x = dice[1]; dice[1] = 7 - dice[2]; dice[2] = x; } if (a[0] == 'R') { x = dice[1]; dice[1] = dice[2]; dice[2] = 7 - x; } t = t + dice[0]; } printf("%d\n", t); } return 0; }
[["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 44]]
1
363
#include <stdio.h> int a[6], t; #define f(A, B, C, D) \ (t = a[D], a[D] = a[C], a[C] = a[B], a[B] = a[A], a[A] = t) int main(void) { int n, r, i; char s[9]; while (scanf("%d", &n), n) { for (r = i = 0; i < 6; ++i) a[i] = i + 1; while (n--) { scanf("%s", s); if (s[0] == 'N') f(0, 4, 5, 1); if (s[0] == 'S') f(0, 1, 5, 4); if (s[0] == 'E') f(0, 2, 5, 3); if (s[0] == 'W') f(0, 3, 5, 2); if (s[0] == 'R') f(1, 3, 4, 2); if (s[0] == 'L') f(1, 2, 4, 3); r += a[0]; } printf("%d\n", r); } return 0; }
#include <stdio.h> int a[6], t; #define f(A, B, C, D) \ (t = a[D], a[D] = a[C], a[C] = a[B], a[B] = a[A], a[A] = t) int main(void) { int n, r, i; char s[9]; while (scanf("%d", &n), n) { for (r = i = 1; i < 7; ++i) a[i - 1] = i; while (n--) { scanf("%s", s); if (s[0] == 'N') f(0, 4, 5, 1); if (s[0] == 'S') f(0, 1, 5, 4); if (s[0] == 'E') f(0, 2, 5, 3); if (s[0] == 'W') f(0, 3, 5, 2); if (s[0] == 'R') f(1, 3, 4, 2); if (s[0] == 'L') f(1, 2, 4, 3); r += a[0]; } printf("%d\n", r); } return 0; }
[["-", 8, 9, 0, 7, 10, 11, 12, 11, 12, 13], ["+", 8, 9, 0, 7, 10, 11, 12, 11, 12, 13], ["-", 0, 52, 8, 9, 0, 7, 15, 16, 12, 13], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 12, 13], ["+", 0, 11, 31, 69, 341, 342, 0, 16, 17, 33], ["+", 0, 11, 31, 69, 341, 342, 0, 16, 12, 13], ["-", 0, 7, 8, 1, 0, 11, 12, 16, 17, 72], ["-", 0, 7, 8, 1, 0, 11, 12, 16, 12, 13]]
1
251
#include <cstdio> using namespace std; int sai[6] = {1, 2, 3, 4, 5, 6}; int fun(char c) { int fsai[6]; if (c == 'N') { fsai[0] = sai[1]; fsai[1] = sai[5]; fsai[2] = sai[2]; fsai[3] = sai[3]; fsai[4] = sai[0]; fsai[5] = sai[4]; } if (c == 'E') { fsai[0] = sai[3]; fsai[1] = sai[1]; fsai[2] = sai[0]; fsai[3] = sai[5]; fsai[4] = sai[4]; fsai[5] = sai[2]; } if (c == 'W') { fsai[0] = sai[2]; fsai[1] = sai[1]; fsai[2] = sai[5]; fsai[3] = sai[0]; fsai[4] = sai[4]; fsai[5] = sai[3]; } if (c == 'S') { fsai[0] = sai[4]; fsai[1] = sai[0]; fsai[2] = sai[2]; fsai[3] = sai[3]; fsai[4] = sai[5]; fsai[5] = sai[1]; } if (c == 'R') { fsai[0] = sai[0]; fsai[1] = sai[2]; fsai[2] = sai[4]; fsai[3] = sai[1]; fsai[4] = sai[3]; fsai[5] = sai[5]; } if (c == 'L') { fsai[0] = sai[0]; fsai[1] = sai[3]; fsai[2] = sai[1]; fsai[3] = sai[4]; fsai[4] = sai[2]; fsai[5] = sai[5]; } for (int i = 0; i < 6; i++) { sai[i] = fsai[i]; } return sai[0]; } int main() { while (true) { for (int i = 0; i < 6; i++) { sai[i] = i; } int n; int answer = 1; scanf("%d", &n); if (n == 0) break; for (int i = 0; i < n; i++) { char c[10]; scanf("%s", c); answer += fun(c[0]); } printf("%d\n", answer); } return 0; }
#include <cstdio> using namespace std; int sai[6] = {1, 2, 3, 4, 5, 6}; int fun(char c) { int fsai[6]; if (c == 'N') { fsai[0] = sai[1]; fsai[1] = sai[5]; fsai[2] = sai[2]; fsai[3] = sai[3]; fsai[4] = sai[0]; fsai[5] = sai[4]; } if (c == 'E') { fsai[0] = sai[3]; fsai[1] = sai[1]; fsai[2] = sai[0]; fsai[3] = sai[5]; fsai[4] = sai[4]; fsai[5] = sai[2]; } if (c == 'W') { fsai[0] = sai[2]; fsai[1] = sai[1]; fsai[2] = sai[5]; fsai[3] = sai[0]; fsai[4] = sai[4]; fsai[5] = sai[3]; } if (c == 'S') { fsai[0] = sai[4]; fsai[1] = sai[0]; fsai[2] = sai[2]; fsai[3] = sai[3]; fsai[4] = sai[5]; fsai[5] = sai[1]; } if (c == 'R') { fsai[0] = sai[0]; fsai[1] = sai[2]; fsai[2] = sai[4]; fsai[3] = sai[1]; fsai[4] = sai[3]; fsai[5] = sai[5]; } if (c == 'L') { fsai[0] = sai[0]; fsai[1] = sai[3]; fsai[2] = sai[1]; fsai[3] = sai[4]; fsai[4] = sai[2]; fsai[5] = sai[5]; } for (int i = 0; i < 6; i++) { sai[i] = fsai[i]; } return sai[0]; } int main() { while (true) { for (int i = 0; i < 6; i++) { sai[i] = i + 1; } int n; int answer = 1; scanf("%d", &n); if (n == 0) break; for (int i = 0; i < n; i++) { char c[10]; scanf("%s", c); answer += fun(c[0]); } printf("%d\n", answer); } return 0; }
[["+", 8, 9, 0, 1, 0, 11, 12, 16, 17, 72], ["+", 8, 9, 0, 1, 0, 11, 12, 16, 12, 13]]
1
607
#include <iostream> #include <string> using namespace std; int main() { string a; int n; int b; //上面 int c; //前面 int d; //右面 int e; int f; int g; while (cin >> n) { if (n != 0) { b = 1; c = 2; d = 3; e = 0; g = 0; while (e < n) { cin >> a; if (a == "North") { f = c; c = 7 - b; b = f; } if (a == "East") { f = b; b = 7 - d; d = f; } if (a == "West") { f = d; d = 7 - b; b = f; } if (a == "South") { f = b; b = 7 - c; c = f; } if (a == "Right") { f = d; d = 7 - c; c = f; } if (a == "Left") { f = c; c = 7 - d; d = f; } g = g + b; e = e + 1; } cout << g << endl; } } }
#include <iostream> #include <string> using namespace std; int main() { string a; int n; int b; //上面 int c; //前面 int d; //右面 int e; int f; int g; while (cin >> n) { if (n != 0) { b = 1; c = 2; d = 3; e = 0; g = 1; while (e < n) { cin >> a; if (a == "North") { f = c; c = 7 - b; b = f; } if (a == "East") { f = b; b = 7 - d; d = f; } if (a == "West") { f = d; d = 7 - b; b = f; } if (a == "South") { f = b; b = 7 - c; c = f; } if (a == "Right") { f = d; d = 7 - c; c = f; } if (a == "Left") { f = c; c = 7 - d; d = f; } g = g + b; e = e + 1; } cout << g << endl; } } }
[["-", 0, 57, 64, 9, 0, 1, 0, 11, 12, 13], ["+", 0, 57, 64, 9, 0, 1, 0, 11, 12, 13]]
1
251
#include <cstdio> #include <iostream> #include <string> using namespace std; int main() { while (1) { string str; int a, buf, N = 5, E = 3, S = 2, W = 4, U = 1, D = 6, answer = 0; cin >> a; if (a == 0) return 0; for (int i = 0; i < a; i++) { cin >> str; if (str == "North") { buf = N; N = U; U = S; S = D; D = buf; } else if (str == "East") { buf = E; E = U; U = W; W = D; D = buf; } else if (str == "West") { buf = W; W = U; U = E; E = D; D = buf; } else if (str == "South") { buf = S; S = U; U = N; N = D; D = buf; } else if (str == "Left") { buf = S; S = W; W = N; N = E; E = buf; } else { buf = S; S = E; E = N; N = W; W = buf; } answer += U; } printf("%d\n", answer); } }
#include <cstdio> #include <iostream> #include <string> using namespace std; int main() { while (1) { string str; int a, buf, N = 5, E = 3, S = 2, W = 4, U = 1, D = 6, answer = 0; cin >> a; if (a == 0) return 0; for (int i = 0; i < a; i++) { cin >> str; if (str == "North") { buf = N; N = U; U = S; S = D; D = buf; } else if (str == "East") { buf = E; E = U; U = W; W = D; D = buf; } else if (str == "West") { buf = W; W = U; U = E; E = D; D = buf; } else if (str == "South") { buf = S; S = U; U = N; N = D; D = buf; } else if (str == "Left") { buf = S; S = W; W = N; N = E; E = buf; } else { buf = S; S = E; E = N; N = W; W = buf; } answer += U; } printf("%d\n", answer + 1); } }
[["+", 0, 1, 0, 2, 3, 4, 0, 16, 17, 72], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13]]
1
282
#include <iostream> #include <string> #include <vector> int main() { int a = 0; int z = 1; while (1) { std::cin >> a; if (a == 0) break; std::vector<int> vec(7); std::vector<int> vec1(7); for (int i = 1; i < 7; i++) { vec[i] = i; vec1[i] = i; } std::string str; for (int i = 0; i < a; i++) { std::cin >> str; if (i % 2 == 0) { if (str == "North") { vec[1] = vec1[2]; vec[2] = vec1[6]; vec[6] = vec1[5]; vec[5] = vec1[1]; vec[3] = vec1[3]; vec[4] = vec1[4]; z = z + vec[1]; } if (str == "East") { vec[1] = vec1[4]; vec[4] = vec1[6]; vec[6] = vec1[3]; vec[3] = vec1[1]; vec[2] = vec1[2]; vec[5] = vec1[5]; z = z + vec[1]; } if (str == "West") { vec[1] = vec1[3]; vec[3] = vec1[6]; vec[6] = vec1[4]; vec[4] = vec1[1]; vec[2] = vec1[2]; vec[5] = vec1[5]; z = z + vec[1]; } if (str == "South") { vec[1] = vec1[5]; vec[5] = vec1[6]; vec[6] = vec1[2]; vec[2] = vec1[1]; vec[3] = vec1[3]; vec[4] = vec1[4]; z = z + vec[1]; } if (str == "Left") { vec[2] = vec1[4]; vec[4] = vec1[5]; vec[5] = vec1[3]; vec[3] = vec1[2]; vec[1] = vec1[1]; vec[6] = vec1[6]; z = z + vec[1]; } if (str == "Right") { vec[2] = vec1[3]; vec[3] = vec1[5]; vec[5] = vec1[4]; vec[4] = vec1[2]; vec[1] = vec1[1]; vec[6] = vec1[6]; z = z + vec[1]; } } if (i % 2 == 1) { if (str == "North") { vec1[1] = vec[2]; vec1[2] = vec[6]; vec1[6] = vec[5]; vec1[5] = vec[1]; vec1[3] = vec[3]; vec1[4] = vec[4]; z = z + vec1[1]; } if (str == "East") { vec1[1] = vec[4]; vec1[4] = vec[6]; vec1[6] = vec[3]; vec1[3] = vec[1]; vec1[2] = vec[2]; vec1[5] = vec[5]; z = z + vec1[1]; } if (str == "West") { vec1[1] = vec[3]; vec1[3] = vec[6]; vec1[6] = vec[4]; vec1[4] = vec[1]; vec1[2] = vec[2]; vec1[5] = vec[5]; z = z + vec1[1]; } if (str == "South") { vec1[1] = vec[5]; vec1[5] = vec[6]; vec1[6] = vec[2]; vec1[2] = vec[1]; vec1[3] = vec[3]; vec1[4] = vec[4]; z = z + vec1[1]; } if (str == "Left") { vec1[2] = vec[4]; vec1[4] = vec[5]; vec1[5] = vec[3]; vec1[3] = vec[2]; vec1[1] = vec[1]; vec1[6] = vec[6]; z = z + vec1[1]; } if (str == "Right") { vec1[2] = vec[3]; vec1[3] = vec[5]; vec1[5] = vec[4]; vec1[4] = vec[2]; vec1[1] = vec[1]; vec1[6] = vec[6]; z = z + vec1[1]; } } } std::cout << z << std::endl; } return 0; }
#include <iostream> #include <string> #include <vector> int main() { int a = 0; int z = 1; while (1) { std::cin >> a; if (a == 0) break; std::vector<int> vec(7); std::vector<int> vec1(7); for (int i = 1; i < 7; i++) { vec[i] = i; vec1[i] = i; } std::string str; for (int i = 0; i < a; i++) { std::cin >> str; if (i % 2 == 0) { if (str == "North") { vec[1] = vec1[2]; vec[2] = vec1[6]; vec[6] = vec1[5]; vec[5] = vec1[1]; vec[3] = vec1[3]; vec[4] = vec1[4]; z = z + vec[1]; } if (str == "East") { vec[1] = vec1[4]; vec[4] = vec1[6]; vec[6] = vec1[3]; vec[3] = vec1[1]; vec[2] = vec1[2]; vec[5] = vec1[5]; z = z + vec[1]; } if (str == "West") { vec[1] = vec1[3]; vec[3] = vec1[6]; vec[6] = vec1[4]; vec[4] = vec1[1]; vec[2] = vec1[2]; vec[5] = vec1[5]; z = z + vec[1]; } if (str == "South") { vec[1] = vec1[5]; vec[5] = vec1[6]; vec[6] = vec1[2]; vec[2] = vec1[1]; vec[3] = vec1[3]; vec[4] = vec1[4]; z = z + vec[1]; } if (str == "Left") { vec[2] = vec1[4]; vec[4] = vec1[5]; vec[5] = vec1[3]; vec[3] = vec1[2]; vec[1] = vec1[1]; vec[6] = vec1[6]; z = z + vec[1]; } if (str == "Right") { vec[2] = vec1[3]; vec[3] = vec1[5]; vec[5] = vec1[4]; vec[4] = vec1[2]; vec[1] = vec1[1]; vec[6] = vec1[6]; z = z + vec[1]; } } if (i % 2 == 1) { if (str == "North") { vec1[1] = vec[2]; vec1[2] = vec[6]; vec1[6] = vec[5]; vec1[5] = vec[1]; vec1[3] = vec[3]; vec1[4] = vec[4]; z = z + vec1[1]; } if (str == "East") { vec1[1] = vec[4]; vec1[4] = vec[6]; vec1[6] = vec[3]; vec1[3] = vec[1]; vec1[2] = vec[2]; vec1[5] = vec[5]; z = z + vec1[1]; } if (str == "West") { vec1[1] = vec[3]; vec1[3] = vec[6]; vec1[6] = vec[4]; vec1[4] = vec[1]; vec1[2] = vec[2]; vec1[5] = vec[5]; z = z + vec1[1]; } if (str == "South") { vec1[1] = vec[5]; vec1[5] = vec[6]; vec1[6] = vec[2]; vec1[2] = vec[1]; vec1[3] = vec[3]; vec1[4] = vec[4]; z = z + vec1[1]; } if (str == "Left") { vec1[2] = vec[4]; vec1[4] = vec[5]; vec1[5] = vec[3]; vec1[3] = vec[2]; vec1[1] = vec[1]; vec1[6] = vec[6]; z = z + vec1[1]; } if (str == "Right") { vec1[2] = vec[3]; vec1[3] = vec[5]; vec1[5] = vec[4]; vec1[4] = vec[2]; vec1[1] = vec[1]; vec1[6] = vec[6]; z = z + vec1[1]; } } } std::cout << z << std::endl; z = 1; } return 0; }
[["+", 0, 52, 8, 9, 0, 1, 0, 11, 31, 22], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 17, 32], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 12, 13], ["+", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35]]
1
1,102
import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class Main { public static class Pair implements Comparable<Pair> { public int first = 0, second = 0; @Override public int compareTo(Pair other) { return this.second - other.second; } } public static void main(String args[]) { Scanner scanner = new Scanner(System.in); int n, m; while (true) { n = scanner.nextInt(); m = scanner.nextInt(); if (n == 0 && m == 0) { break; } Pair[] a = new Pair[m]; for (int i = 0; i < m; i++) { a[i] = new Pair(); a[i].first = i + 1; } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { a[j].second += scanner.nextInt(); } } Arrays.sort(a); for (int i = 0; i < m; i++) System.out.print(a[i].first + (i == m - 1 ? "" : " ")); System.out.println(); } } }
import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class Main { public static class Pair implements Comparable<Pair> { public int first = 0, second = 0; @Override public int compareTo(Pair other) { return other.second - this.second; } } public static void main(String args[]) { Scanner scanner = new Scanner(System.in); int n, m; while (true) { n = scanner.nextInt(); m = scanner.nextInt(); if (n == 0 && m == 0) { break; } Pair[] a = new Pair[m]; for (int i = 0; i < m; i++) { a[i] = new Pair(); a[i].first = i + 1; } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { a[j].second += scanner.nextInt(); } } Arrays.sort(a); for (int i = 0; i < m; i++) System.out.print(a[i].first + (i == m - 1 ? "" : " ")); System.out.println(); } } }
[["-", 8, 196, 0, 37, 0, 16, 31, 509, 500, 274], ["+", 8, 196, 0, 37, 0, 16, 31, 509, 500, 22], ["-", 8, 196, 0, 37, 0, 16, 12, 509, 500, 22], ["+", 8, 196, 0, 37, 0, 16, 12, 509, 500, 274]]
3
282
#include <algorithm> #include <iostream> using namespace std; int N, M, v; pair<int, int> vote_area[110]; int main() { while (cin >> N >> M && N > 0) { for (int l = 0; l < M; ++l) { vote_area[l].first = 0; vote_area[l].second = -(l + 1); } for (int i = 0; i < N; ++i) { for (int j = 0; j < M; ++j) { cin >> v; // i人目の人が場所jに行きたいかどうか vote_area[j].first += v; } } //ここで計算して出力する sort(vote_area, vote_area + M); reverse(vote_area, vote_area + M); for (int t = 0; t < M; ++t) { vote_area[t].second = (-1) * vote_area[t].second; } for (int k = 0; k < M - 1; ++k) { std::cout << vote_area[k].second << std::flush; } cout << vote_area[M - 1].second << endl; } }
#include <algorithm> #include <iostream> using namespace std; int N, M, v; pair<int, int> vote_area[110]; int main() { while (cin >> N >> M && N > 0) { for (int l = 0; l < M; ++l) { vote_area[l].first = 0; vote_area[l].second = -(l + 1); } for (int i = 0; i < N; ++i) { for (int j = 0; j < M; ++j) { cin >> v; // i人目の人が場所jに行きたいかどうか vote_area[j].first += v; } } //ここで計算して出力する sort(vote_area, vote_area + M); reverse(vote_area, vote_area + M); for (int t = 0; t < M; ++t) { vote_area[t].second = (-1) * vote_area[t].second; } for (int k = 0; k < M - 1; ++k) { std::cout << vote_area[k].second << " " << std::flush; } cout << vote_area[M - 1].second << endl; } }
[["+", 0, 1, 0, 16, 31, 16, 12, 5, 0, 62], ["+", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["+", 0, 7, 8, 9, 0, 1, 0, 16, 17, 151]]
1
230
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <utility> #include <vector> using namespace std; typedef vector<int> vi; typedef pair<int, int> pii; typedef long long ll; #define dump(x) cerr << #x << " = " << (x) << endl #define rep(i, n) for (int i = 0; i < (n); i++) #define all(a) (a).begin(), (a).end() #define pb push_back int main() { int n, m; while (cin >> n >> m) { if (n == 0 && m == 0) break; int data[200]; rep(i, n) { rep(j, m) { int x; cin >> x; data[j] += x; } } int num[200] = {}; rep(i, m) num[i] = i; rep(i, m) { for (int j = m - 1; j > i; j--) { if (data[j] > data[j - 1]) { swap(data[j], data[j - 1]); swap(num[j], num[j - 1]); } } } rep(i, m) { cout << num[i] + 1; if (i != m - 1) cout << " "; } cout << endl; } return 0; }
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <utility> #include <vector> using namespace std; typedef vector<int> vi; typedef pair<int, int> pii; typedef long long ll; #define dump(x) cerr << #x << " = " << (x) << endl #define rep(i, n) for (int i = 0; i < (n); i++) #define all(a) (a).begin(), (a).end() #define pb push_back int main() { int n, m; while (cin >> n >> m) { if (n == 0 && m == 0) break; int data[200] = {}; rep(i, n) { rep(j, m) { int x; cin >> x; data[j] += x; } } int num[200] = {}; rep(i, m) num[i] = i; rep(i, m) { for (int j = m - 1; j > i; j--) { if (data[j] > data[j - 1]) { swap(data[j], data[j - 1]); swap(num[j], num[j - 1]); } } } rep(i, m) { cout << num[i] + 1; if (i != m - 1) cout << " "; } cout << endl; } return 0; }
[["+", 0, 52, 8, 9, 0, 43, 49, 50, 0, 32], ["+", 8, 9, 0, 43, 49, 50, 51, 83, 0, 45], ["+", 8, 9, 0, 43, 49, 50, 51, 83, 0, 46]]
1
287
#include <algorithm> #include <iostream> #include <vector> using namespace std; int n, m, c; int main() { while (true) { scanf("%d", &n); scanf("%d", &m); if (n == 0 && m == 0) { break; } vector<pair<int, int>> r(m); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { scanf("%d", &c); r[j].first -= c; } } for (int i = 0; i < m; i++) { r[i].second = i + 1; } sort(r.begin(), r.end()); for (int i = 0; i < m; i++) { if (i) { printf(" "); } printf("%d", r[i].second); } } return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; int n, m, c; int main() { while (true) { scanf("%d", &n); scanf("%d", &m); if (n == 0 && m == 0) { break; } vector<pair<int, int>> r(m); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { scanf("%d", &c); r[j].first -= c; } } for (int i = 0; i < m; i++) { r[i].second = i + 1; } sort(r.begin(), r.end()); for (int i = 0; i < m; i++) { if (i) { printf(" "); } printf("%d", r[i].second); } printf("\n"); } return 0; }
[["+", 0, 52, 8, 9, 0, 1, 0, 2, 63, 22], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 24], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 62], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 44], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 25], ["+", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35]]
1
216
#include <algorithm> #include <iostream> #include <vector> using namespace std; class HOTEL { public: int hotel; int vote; bool operator<(const HOTEL &h) { if (vote == h.vote) { return hotel > h.hotel; } else { return vote < h.vote; } } }; int main() { int i, j, n, m, tmp; while (1) { cin >> n >> m; if (n == 0 && m == 0) break; vector<HOTEL> v; int hton[102]; for (i = 0; i < n; i++) { for (j = 1; j <= m; j++) { cin >> tmp; hton[j] += tmp; } } HOTEL tpair; for (j = 1; j <= m; j++) { tpair.vote = hton[j]; tpair.hotel = j; v.push_back(tpair); } sort(v.begin(), v.end()); int tans; for (i = m - 1; i >= 0; i--) { cout << v[i].hotel; if (i != 0) cout << " "; } cout << endl; } return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; class HOTEL { public: int hotel; int vote; bool operator<(const HOTEL &h) { if (vote == h.vote) { return hotel > h.hotel; } else { return vote < h.vote; } } }; int main() { int i, j, n, m, tmp; while (1) { cin >> n >> m; if (n == 0 && m == 0) break; vector<HOTEL> v; int hton[102] = {}; for (i = 0; i < n; i++) { for (j = 1; j <= m; j++) { cin >> tmp; hton[j] += tmp; } } HOTEL tpair; for (j = 1; j <= m; j++) { tpair.vote = hton[j]; tpair.hotel = j; v.push_back(tpair); } sort(v.begin(), v.end()); int tans; for (i = m - 1; i >= 0; i--) { cout << v[i].hotel; if (i != 0) cout << " "; } cout << endl; } return 0; }
[["+", 0, 52, 8, 9, 0, 43, 49, 50, 0, 32], ["+", 8, 9, 0, 43, 49, 50, 51, 83, 0, 45], ["+", 8, 9, 0, 43, 49, 50, 51, 83, 0, 46]]
1
258
#include <bits/stdc++.h> #define N 105 using namespace std; typedef pair<int, int> P; int n, m, a; P ans[N]; int main() { while (1) { cin >> n >> m; if (!n && !m) break; for (int i = 1; i <= m; i++) ans[i].first = 0, ans[i].second = i; for (int i = 0; i < n; i++) for (int j = 1; j <= m; j++) cin >> a, ans[j].first += a; for (int i = 1; i < m; i++) for (int j = i; j < m; j++) if (ans[j].first < ans[j + 1].first) swap(ans[j], ans[j + 1]); for (int i = 1; i <= m; i++) { if (i != 1) cout << ' '; cout << ans[i].second; } cout << endl; } return 0; }
#include <bits/stdc++.h> #define N 105 using namespace std; typedef pair<int, int> P; int n, m, a; P ans[N]; int main() { while (1) { cin >> n >> m; if (!n && !m) break; for (int i = 1; i <= m; i++) ans[i].first = 0, ans[i].second = i; for (int i = 0; i < n; i++) for (int j = 1; j <= m; j++) cin >> a, ans[j].first += a; for (int i = 1; i <= m; i++) for (int j = 1; j < m; j++) if (ans[j].first < ans[j + 1].first) swap(ans[j], ans[j + 1]); for (int i = 1; i <= m; i++) { if (i != 1) cout << ' '; cout << ans[i].second; } cout << endl; } return 0; }
[["-", 0, 52, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 17, 19], ["-", 0, 7, 8, 7, 10, 43, 49, 50, 51, 22], ["+", 0, 7, 8, 7, 10, 43, 49, 50, 51, 13]]
1
237
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { int n, m; while (cin >> n >> m, n) { vector<pair<int, int>> vp(m); for (int i = 0; i < m; i++) vp[i] = make_pair(0, i); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int t; cin >> t; vp[j].first -= t; } } sort(vp.begin(), vp.end()); cout << vp[0].second; for (int i = 1; i < m; i++) cout << " " << vp[i].second; cout << endl; } }
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { int n, m; while (cin >> n >> m, n) { vector<pair<int, int>> vp(m); for (int i = 0; i < m; i++) vp[i] = make_pair(0, i + 1); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int t; cin >> t; vp[j].first -= t; } } sort(vp.begin(), vp.end()); cout << vp[0].second; for (int i = 1; i < m; i++) cout << " " << vp[i].second; cout << endl; } }
[["+", 0, 11, 12, 2, 3, 4, 0, 16, 17, 72], ["+", 0, 11, 12, 2, 3, 4, 0, 16, 12, 13]]
1
176
#include <algorithm> #include <cstdio> #include <vector> using namespace std; struct A { int n; int a; }; struct A V_A[100]; class sort_ { public: bool operator()(const A &a, const A &b) { if (b.n == a.n) { return a.a < b.a; } return b.n < a.n; } }; int main() { int n, m; while (true) { for (int i = 0; i < 100; i++) { V_A[i].n = 0; V_A[i].a = i; } scanf("%d%d", &n, &m); if (n == 0 && m == 0) break; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int k = 0; scanf("%d", &k); V_A[j].n += k; } } sort(V_A, V_A + sizeof(A) * m, sort_()); for (int i = 0; i < m; i++) { if (i != 0) printf(" "); printf("%d", V_A[i].a + 1); } printf("\n"); } return 0; }
#include <algorithm> #include <cstdio> #include <vector> using namespace std; struct A { int n; int a; }; struct A V_A[105]; class sort_ { public: bool operator()(const A &a, const A &b) { if (b.n == a.n) { return a.a < b.a; } return b.n < a.n; } }; int main() { int n, m; while (true) { for (int i = 0; i < 100; i++) { V_A[i].n = 0; V_A[i].a = i; } scanf("%d%d", &n, &m); if (n == 0 && m == 0) break; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int k = 0; scanf("%d", &k); V_A[j].n += k; } } sort(V_A, V_A + m, sort_()); for (int i = 0; i < m; i++) { if (i != 0) printf(" "); printf("%d", V_A[i].a + 1); } printf("\n"); } return 0; }
[["-", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13], ["+", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13], ["-", 0, 2, 3, 4, 0, 16, 12, 105, 0, 150], ["-", 0, 16, 12, 105, 51, 16, 31, 23, 0, 24], ["-", 0, 16, 12, 105, 51, 16, 31, 23, 0, 22], ["-", 0, 16, 12, 105, 51, 16, 31, 23, 0, 25], ["-", 3, 4, 0, 16, 12, 105, 51, 16, 17, 48]]
1
286
#include <algorithm> #include <cstdio> #include <iostream> #include <map> using namespace std; typedef pair<int, int> P; P p[111]; int main(void) { while (1) { int n, m; cin >> n >> m; if (!n && !m) break; for (int i = 0; i < m; i++) { p[i] = P(0, i + 1); } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int a; cin >> a; p[j].first -= a; } } sort(p, p + m); for (int i = 0; i < m; i++) { cout << p[i].second; if (i == m) cout << endl; else cout << " "; } } }
#include <algorithm> #include <cstdio> #include <iostream> #include <map> using namespace std; typedef pair<int, int> P; P p[111]; int main(void) { while (1) { int n, m; cin >> n >> m; if (!n && !m) break; for (int i = 0; i < m; i++) { p[i] = P(0, i + 1); } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int a; cin >> a; p[j].first -= a; } } sort(p, p + m); for (int i = 0; i < m; i++) { cout << p[i].second; if (i == m - 1) cout << endl; else cout << " "; } } }
[["+", 0, 57, 15, 339, 51, 16, 12, 16, 17, 33], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 12, 13]]
1
190
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <map> #include <string> #include <vector> #define loop(i, a, b) for (int i = a; i < b; i++) #define rep(i, a) loop(i, 0, a) #define pb(in, tmp) in.push_back(tmp) #define all(in) in.begin(), n.end() #define PI acos(-1) using namespace std; int main() { int n, m; while (cin >> n >> m, n) { vector<int> a(m); vector<int> b(m, 0); rep(i, m) a[i] = i; rep(i, n) { rep(j, m) { int tmp; cin >> tmp; b[j] += tmp; } } // cout<<b[0]<<endl;; rep(i, m - 1) { rep(j, m - 1) { if (b[j] < b[j + 1]) { swap(b[i], b[j]); swap(a[i], a[j]); } } } rep(i, m) { if (i == m - 1) cout << ++a[i] << endl; else cout << ++a[i] << " "; } } }
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <map> #include <string> #include <vector> #define loop(i, a, b) for (int i = a; i < b; i++) #define rep(i, a) loop(i, 0, a) #define pb(in, tmp) in.push_back(tmp) #define all(in) in.begin(), n.end() #define PI acos(-1) using namespace std; int main() { int n, m; while (cin >> n >> m, n) { vector<int> a(m); vector<int> b(m, 0); rep(i, m) a[i] = i; rep(i, n) { rep(j, m) { int tmp; cin >> tmp; b[j] += tmp; } } // cout<<b[0]<<endl;; rep(i, m - 1) { rep(j, m - 1) { if (b[j] < b[j + 1]) { swap(b[j], b[j + 1]); swap(a[j], a[j + 1]); } } } rep(i, m) { if (i == m - 1) cout << ++a[i] << endl; else cout << ++a[i] << " "; } } }
[["-", 0, 2, 3, 4, 0, 69, 341, 342, 0, 22], ["+", 0, 2, 3, 4, 0, 69, 341, 342, 0, 22], ["+", 3, 4, 0, 69, 341, 342, 0, 16, 17, 72], ["+", 3, 4, 0, 69, 341, 342, 0, 16, 12, 13]]
1
243
while True: students,places = list(map(int,input().split())) if students == 0 and places == 0: break res = dict(list(zip(list(range(places)),[0]*places))) for i in range(students): points = list(map(int,input().split())) for j in range(places): res[j] += points[j] for k,v in sorted(list(res.items()),key=lambda x:x[1], reverse=True): print(k+1, end=' ') print("\n")
while True: students,places = list(map(int,input().split())) if students == 0 and places == 0: break res = dict(list(zip(list(range(places)),[0]*places))) for i in range(students): points = list(map(int,input().split())) for j in range(places): res[j] += points[j] for k,v in sorted(list(res.items()),key=lambda x:x[1], reverse=True): print(k+1, end=' ') print()
[["-", 0, 1, 0, 652, 3, 4, 0, 557, 0, 654], ["-", 0, 652, 3, 4, 0, 557, 0, 6, 0, 44], ["-", 0, 1, 0, 652, 3, 4, 0, 557, 0, 655]]
5
147
/* AOJ 0507 Title:Square @kankichi573 */ #include <stdio.h> #define min(x, y) (((x) < (y)) ? (x) : (y)) int prefix[31]; void print_prefix(int len) { int i; for (i = 0; i < len; i++) printf("%d ", prefix[i]); } void print_square(int sum, int len, int max_) { int i; // printf(":%d %d %d\n",sum,len,max_); if (sum <= 0) return; if (sum <= max_) { print_prefix(len); printf("%d\n", sum); } else if (1 == max_ || 1 == sum) { print_prefix(len); for (i = 0; i < sum; i++) { printf("1"); if (i < sum - 1) printf(" "); } printf("\n"); } if (max_ > 1) for (i = min(max_ - 1, sum - 1); i >= 1; i--) { prefix[len] = i; print_square(sum - i, len + 1, i); } } main() { int i, ret, N; while (scanf("%d", &N) && (N)) print_square(N, 0, N); return (0); }
/* AOJ 0507 Title:Square @kankichi573 */ #include <stdio.h> #define min(x, y) (((x) < (y)) ? (x) : (y)) int prefix[31]; void print_prefix(int len) { int i; for (i = 0; i < len; i++) printf("%d ", prefix[i]); } void print_square(int sum, int len, int max_) { int i; // printf(":%d %d %d\n",sum,len,max_); if (sum <= 0) return; if (sum <= max_) { print_prefix(len); printf("%d\n", sum); } else if (1 == max_ || 1 == sum) { print_prefix(len); for (i = 0; i < sum; i++) { printf("1"); if (i < sum - 1) printf(" "); } printf("\n"); } if (max_ > 1) for (i = min(max_, sum - 1); i >= 1; i--) { prefix[len] = i; print_square(sum - i, len + 1, i); } } main() { int i, ret, N; while (scanf("%d", &N) && (N)) print_square(N, 0, N); return (0); }
[["-", 10, 11, 12, 2, 3, 4, 0, 16, 17, 33], ["-", 10, 11, 12, 2, 3, 4, 0, 16, 12, 13]]
0
257
#include <memory.h> #include <stdio.h> void f(int now, int rest, int list[], int p) { int i; list[p] = now; if (rest == 0) { for (i = 0; i <= p; i++) { if (i > 0) { printf(" "); } printf("%d", list[i]); } } for (i = rest; i > 0; i--) { if (i > now) { continue; } f(i, rest - i, list, p + 1); } } int main(void) { int n, i, list[301]; while (1) { scanf("%d", &n); if (n == 0) { break; } memset(list, 0, sizeof(list)); for (i = n; i > 0; i--) { f(i, n - i, list, 0); } } return 0; }
#include <memory.h> #include <stdio.h> void f(int now, int rest, int list[], int p) { int i; list[p] = now; if (rest == 0) { for (i = 0; i <= p; i++) { if (i > 0) { printf(" "); } printf("%d", list[i]); } puts(""); } for (i = rest; i > 0; i--) { if (i > now) { continue; } f(i, rest - i, list, p + 1); } } int main(void) { int n, i, list[301]; while (1) { scanf("%d", &n); if (n == 0) { break; } memset(list, 0, sizeof(list)); for (i = n; i > 0; i--) { f(i, n - i, list, 0); } } return 0; }
[["+", 0, 57, 64, 9, 0, 1, 0, 2, 63, 22], ["+", 64, 9, 0, 1, 0, 2, 3, 4, 0, 24], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 62], ["+", 64, 9, 0, 1, 0, 2, 3, 4, 0, 25], ["+", 8, 9, 0, 57, 64, 9, 0, 1, 0, 35]]
0
209
#include <algorithm> #include <cstring> #include <iostream> #include <map> #include <set> #include <sstream> #include <string> #include <vector> #define REP(i, k, n) for (int i = k; i < n; i++) #define rep(i, n) for (int i = 0; i < n; i++) #define INF 1 << 30 #define pb push_back #define mp make_pair using namespace std; typedef long long ll; typedef pair<int, int> P; int n; vector<vector<int>> ans; void dfs(vector<int> v, int t, int sum) { if (sum == n) { ans.push_back(v); return; } for (int i = t; i >= 1; i--) { if (sum + i <= n) { vector<int> ret(v.begin(), v.end()); ret.push_back(i); dfs(ret, i, sum + i); } } return; } int main() { while (cin >> n && n) { vector<int> v; dfs(v, n, 0); rep(i, ans.size()) { vector<int> t = ans[i]; rep(j, t.size()) { cout << t[j]; if (j == t.size() - 1) cout << endl; else cout << " "; } } } return 0; }
#include <algorithm> #include <cstring> #include <iostream> #include <map> #include <set> #include <sstream> #include <string> #include <vector> #define REP(i, k, n) for (int i = k; i < n; i++) #define rep(i, n) for (int i = 0; i < n; i++) #define INF 1 << 30 #define pb push_back #define mp make_pair using namespace std; typedef long long ll; typedef pair<int, int> P; int n; vector<vector<int>> ans; void dfs(vector<int> v, int t, int sum) { if (sum == n) { ans.push_back(v); return; } for (int i = t; i >= 1; i--) { if (sum + i <= n) { vector<int> ret(v.begin(), v.end()); ret.push_back(i); dfs(ret, i, sum + i); } } return; } int main() { while (cin >> n && n) { ans.clear(); vector<int> v; dfs(v, n, 0); rep(i, ans.size()) { vector<int> t = ans[i]; rep(j, t.size()) { cout << t[j]; if (j == t.size() - 1) cout << endl; else cout << " "; } } } return 0; }
[["+", 8, 9, 0, 1, 0, 2, 63, 118, 28, 22], ["+", 8, 9, 0, 1, 0, 2, 63, 118, 17, 131], ["+", 8, 9, 0, 1, 0, 2, 63, 118, 119, 120], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 24], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 25], ["+", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35]]
1
273
#include "bits/stdc++.h" #include <unordered_map> #include <unordered_set> #pragma warning(disable : 4996) using namespace std; using ld = long double; template <class T> using Table = vector<vector<T>>; vector<vector<int>> ans; void getans(vector<int> now, int pre, int rest) { for (int i = min(rest, pre); i >= 1; --i) { vector<int> v(now); v.push_back(i); if (i == rest) ans.push_back(v); else { getans(v, i, rest - i); } } } int main() { while (1) { int N; cin >> N; if (!N) break; getans(vector<int>(), N, N); for (auto v : ans) { for (int i = 0; i < v.size(); ++i) { cout << v[i]; if (i != v.size() - 1) cout << " "; else cout << endl; } } } return 0; }
#include "bits/stdc++.h" #include <unordered_map> #include <unordered_set> #pragma warning(disable : 4996) using namespace std; using ld = long double; template <class T> using Table = vector<vector<T>>; vector<vector<int>> ans; void getans(vector<int> now, int pre, int rest) { for (int i = min(rest, pre); i >= 1; --i) { vector<int> v(now); v.push_back(i); if (i == rest) ans.push_back(v); else { getans(v, i, rest - i); } } } int main() { while (1) { ans.clear(); int N; cin >> N; if (!N) break; getans(vector<int>(), N, N); for (auto v : ans) { for (int i = 0; i < v.size(); ++i) { cout << v[i]; if (i != v.size() - 1) cout << " "; else cout << endl; } } } return 0; }
[["+", 8, 9, 0, 1, 0, 2, 63, 118, 28, 22], ["+", 8, 9, 0, 1, 0, 2, 63, 118, 17, 131], ["+", 8, 9, 0, 1, 0, 2, 63, 118, 119, 120], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 24], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 25], ["+", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35]]
1
228
#include <algorithm> #include <array> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <functional> #include <iostream> #include <limits.h> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <unordered_map> #include <vector> #define rep(i, s, n) for (int i = (s); (n) > i; i++) #define REP(i, n) rep(i, 0, n) #define RANGE(x, a, b) ((a) <= (x) && (x) <= (b)) #define DUPLE(a, b, c, d) \ (RANGE(a, c, d) || RANGE(b, c, d) || RANGE(c, a, b) || RANGE(d, a, b)) #define INCLU(a, b, c, d) (RANGE(a, c, d) && (b, c, d)) #define PW(x) ((x) * (x)) #define ALL(x) (x).begin(), (x).end() #define RALL(x) (x).rbegin(), (x).rend() #define MODU 1000000007 #define bitcheck(a, b) ((a >> b) & 1) #define bitset(a, b) (a |= (1 << b)) #define bitunset(a, b) (a &= ~(1 << b)) #define MP(a, b) make_pair((a), (b)) #define Manh(a, b) (abs((a).first-(b).first) + abs((a).second - ((b).second)) #define pritnf printf #define scnaf scanf #define itn int #define PI 3.141592653589 #define izryt bool using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; template <typename A, size_t N, typename T> void Fill(A (&array)[N], const T &val) { std::fill((T *)array, (T *)(array + N), val); } pii Dir[8] = { //?§???? {0, 1}, {-1, 0}, {1, 0}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; signed main() { int n; while (scanf("%d", &n)) { if (!n) break; vector<int> used; function<void(int, int)> func = [&](int sum, int bef) { if (sum == n) { REP(i, used.size()) { printf("%d", used[i]); if (i != (int)used.size() - 1) printf(" "); } printf("\n"); return; } for (int i = bef; 1 <= bef; bef--) { if (sum + i <= n) { used.push_back(i); func(sum + i, i); used.erase(--used.end()); } } }; func(0, n); } return 0; }
#include <algorithm> #include <array> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <functional> #include <iostream> #include <limits.h> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <unordered_map> #include <vector> #define rep(i, s, n) for (int i = (s); (n) > i; i++) #define REP(i, n) rep(i, 0, n) #define RANGE(x, a, b) ((a) <= (x) && (x) <= (b)) #define DUPLE(a, b, c, d) \ (RANGE(a, c, d) || RANGE(b, c, d) || RANGE(c, a, b) || RANGE(d, a, b)) #define INCLU(a, b, c, d) (RANGE(a, c, d) && (b, c, d)) #define PW(x) ((x) * (x)) #define ALL(x) (x).begin(), (x).end() #define RALL(x) (x).rbegin(), (x).rend() #define MODU 1000000007 #define bitcheck(a, b) ((a >> b) & 1) #define bitset(a, b) (a |= (1 << b)) #define bitunset(a, b) (a &= ~(1 << b)) #define MP(a, b) make_pair((a), (b)) #define Manh(a, b) (abs((a).first-(b).first) + abs((a).second - ((b).second)) #define pritnf printf #define scnaf scanf #define itn int #define PI 3.141592653589 #define izryt bool using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; template <typename A, size_t N, typename T> void Fill(A (&array)[N], const T &val) { std::fill((T *)array, (T *)(array + N), val); } pii Dir[8] = { //?§???? {0, 1}, {-1, 0}, {1, 0}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; signed main() { int n; while (scanf("%d", &n)) { if (!n) break; vector<int> used; function<void(int, int)> func = [&](int sum, int bef) { if (sum == n) { REP(i, used.size()) { printf("%d", used[i]); if (i != (int)used.size() - 1) printf(" "); } printf("\n"); return; } for (int i = bef; 1 <= i; i--) { if (sum + i <= n) { used.push_back(i); func(sum + i, i); used.erase(--used.end()); } } }; func(0, n); } return 0; }
[["-", 51, 218, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 51, 218, 8, 9, 0, 7, 15, 16, 12, 22], ["-", 51, 218, 8, 9, 0, 7, 26, 27, 28, 22], ["+", 51, 218, 8, 9, 0, 7, 26, 27, 28, 22]]
1
488
#include <cstdio> int calc(int aa[31], int l) { int a[31], f = 1; for (int i = 0; i < 31; i++) a[i] = aa[i]; for (int i = 1; i < l + 1; i++) if (a[i - 1] < a[i]) f = 0; for (int i = 0; f && i < l; i++) printf("%d ", a[i]); if (f) printf("%d\n", a[l]); while (a[l] > 1) { if (a[l - 1] < a[l]) { a[l + 1] += a[l] - a[l - 1]; a[l] -= a[l] - a[l - 1]; } else a[l]--, a[l + 1]++; calc(a, l + 1); } return 0; } int main() { int n; int a[31]; while (scanf("%d", &n), n) { for (int i = 0; i < 31; i++) a[i] = 0; a[0] = n; printf("%d\n", n); for (int i = 0; i < n; i++) { a[0]--; a[1]++; calc(a, 1); } } return 0; }
#include <cstdio> int calc(int aa[31], int l) { int a[31], f = 1; for (int i = 0; i < 31; i++) a[i] = aa[i]; for (int i = 1; i < l + 1; i++) if (a[i - 1] < a[i]) f = 0; for (int i = 0; f && i < l; i++) printf("%d ", a[i]); if (f) printf("%d\n", a[l]); while (a[l] > 1) { if (a[l - 1] < a[l]) { a[l + 1] += a[l] - a[l - 1]; a[l] -= a[l] - a[l - 1]; } else a[l]--, a[l + 1]++; calc(a, l + 1); } return 0; } int main() { int n; int a[31]; while (scanf("%d", &n), n) { for (int i = 0; i < 31; i++) a[i] = 0; a[0] = n; printf("%d\n", n); for (int i = 0; i < n - 1; i++) { a[0]--; a[1]++; calc(a, 1); } } return 0; }
[["+", 8, 9, 0, 7, 15, 16, 12, 16, 17, 33], ["+", 8, 9, 0, 7, 15, 16, 12, 16, 12, 13]]
1
326
#include <iostream> using namespace std; int a[31], n; void hyo(int sum = 0, int size = 0) { if (sum == n) { for (int i = 0; i < size; i++) { if (i) cout << " "; cout << a[i]; } cout << endl; } for (int i = n - sum; i > 0; i--) { a[size] = i; if (sum != 0) if (i > a[size - 1]) continue; hyo(i + sum, size + 1); } } int main() { cin >> n; hyo(); return 0; }
#include <iostream> using namespace std; int a[31], n; void hyo(int sum = 0, int size = 0) { if (sum == n) { for (int i = 0; i < size; i++) { if (i) cout << " "; cout << a[i]; } cout << endl; } for (int i = n - sum; i > 0; i--) { a[size] = i; if (sum != 0) if (i > a[size - 1]) continue; hyo(i + sum, size + 1); } } int main() { while (cin >> n, n) hyo(); return 0; }
[["+", 0, 30, 0, 14, 8, 9, 0, 52, 0, 89], ["+", 0, 14, 8, 9, 0, 52, 15, 339, 0, 24], ["-", 0, 30, 0, 14, 8, 9, 0, 1, 0, 35], ["+", 8, 9, 0, 52, 15, 339, 51, 34, 0, 21], ["+", 8, 9, 0, 52, 15, 339, 51, 34, 12, 22], ["+", 0, 14, 8, 9, 0, 52, 15, 339, 0, 25]]
1
146
#include <iostream> #include <sstream> using namespace std; int n; int r(int a, int b, string s) { a || cout << s; for (int i = a < b ? a : b; i > 0; i--) { stringstream t; t << i << (a - i ? " " : "\n"); r(a - i, i, s + t.str()); } } main() { cin >> n; r(n, n, ""); }
#include <iostream> #include <sstream> using namespace std; int n; int r(int a, int b, string s) { a || cout << s; for (int i = a < b ? a : b; i > 0; i--) { stringstream t; t << i << (a - i ? " " : "\n"); r(a - i, i, s + t.str()); } } main() { for (; cin >> n, n;) r(n, n, ""); }
[["+", 0, 30, 0, 14, 8, 9, 0, 7, 0, 88], ["+", 0, 30, 0, 14, 8, 9, 0, 7, 0, 24], ["+", 0, 30, 0, 14, 8, 9, 0, 7, 0, 35], ["+", 8, 9, 0, 7, 15, 34, 31, 16, 12, 22], ["+", 0, 14, 8, 9, 0, 7, 15, 34, 0, 21], ["+", 0, 30, 0, 14, 8, 9, 0, 7, 0, 25]]
1
110
#include <algorithm> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <functional> #include <iostream> #include <map> #include <memory> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #ifdef _MSC_VER #define __typeof__ decltype template <class T> int __builtin_popcount(T n) { return n ? 1 + __builtin_popcount(n & (n - 1)) : 0; } #endif #define foreach(it, c) \ for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it) #define all(c) (c).begin(), (c).end() #define rall(c) (c).rbegin(), (c).rend() #define popcount __builtin_popcount const double EPS = 1e-10; typedef long long ll; typedef pair<int, int> pint; int main() { int n; while (scanf("%d", &n), n) { int b[16]; memset(b, 0, sizeof(b)); b[0] = n; while (true) { int t = find(b, b + n, 0) - b; for (int i = 0; i < t - 1; ++i) printf("%d ", b[i]); printf("%d\n", b[t - 1]); if (t == n) break; int u; u = t - 1; while (b[u] == 1) --u; --b[u]; ++u; fill(b + u, b + n, 0); int s = t - u + 1; while (s--) { if (b[u] == b[u - 1]) ++u; ++b[u]; } } } }
#include <algorithm> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <functional> #include <iostream> #include <map> #include <memory> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #ifdef _MSC_VER #define __typeof__ decltype template <class T> int __builtin_popcount(T n) { return n ? 1 + __builtin_popcount(n & (n - 1)) : 0; } #endif #define foreach(it, c) \ for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it) #define all(c) (c).begin(), (c).end() #define rall(c) (c).rbegin(), (c).rend() #define popcount __builtin_popcount const double EPS = 1e-10; typedef long long ll; typedef pair<int, int> pint; int main() { int n; while (scanf("%d", &n), n) { int b[31]; memset(b, 0, sizeof(b)); b[0] = n; while (true) { int t = find(b, b + n, 0) - b; for (int i = 0; i < t - 1; ++i) printf("%d ", b[i]); printf("%d\n", b[t - 1]); if (t == n) break; int u; u = t - 1; while (b[u] == 1) --u; --b[u]; ++u; fill(b + u, b + n, 0); int s = t - u + 1; while (s--) { if (b[u] == b[u - 1]) ++u; ++b[u]; } } } }
[["-", 0, 52, 8, 9, 0, 43, 49, 80, 81, 13], ["+", 0, 52, 8, 9, 0, 43, 49, 80, 81, 13]]
1
332
INF = 2 << 32 def out stat, remain puts stat.join(' ') if remain.zero? ([stat.last || INF, remain].min.downto(1)).each {|n| out(stat << n, remain - n) } stat.pop end until (n = gets.to_i) == 0 out [], 5 end
INF = 2 << 32 def out stat, remain puts stat.join(' ') if remain.zero? ([stat.last || INF, remain].min.downto(1)).each {|n| out(stat << n, remain - n) } stat.pop end until (n = gets.to_i) == 0 out [], n end
[["-", 0, 750, 8, 170, 0, 652, 3, 4, 0, 612], ["+", 0, 750, 8, 170, 0, 652, 3, 4, 0, 22]]
4
78
l = [0 for i in range(20)] pos = goal = 0 def solve(cur,sum): global pos, goal if cur == goal: print(" ".join(map(str,l[:pos]))) return for i in range(sum,0,-1): if cur + i <= goal: l[pos] = i pos += 1 solve(cur+i,i) pos -= 1 while True: goal = int(input()) if goal==0: break solve(0,goal)
l = [0 for i in range(31)] pos = goal = 0 def solve(cur,sum): global pos, goal if cur == goal: print(" ".join(map(str,l[:pos]))) return for i in range(sum,0,-1): if cur + i <= goal: l[pos] = i pos += 1 solve(cur+i,i) pos -= 1 while True: goal = int(input()) if goal==0: break solve(0,goal)
[["-", 12, 658, 0, 659, 12, 652, 3, 4, 0, 612], ["+", 12, 658, 0, 659, 12, 652, 3, 4, 0, 612]]
5
118
#include <bitset> #include <iostream> #include <vector> using namespace std; class range { private: struct Iterator { int val; int operator*() { return val; } bool operator!=(Iterator &itr) { return val < itr.val; } void operator++() { ++val; } }; Iterator i, n; public: range(int n) : i({0}), n({n}) {} range(int i, int n) : i({i}), n({n}) {} Iterator &begin() { return i; } Iterator &end() { return n; } }; template <class T> T at(vector<T> v, int i) { return v[(i % (int)v.size() + v.size()) % v.size()]; } typedef int Weight; struct Edge { int from, to; Weight weight; Edge(int from, int to, Weight weight) : from(from), to(to), weight(weight) {} }; typedef vector<Edge> Edges; typedef vector<Edges> Graph; typedef vector<Weight> Array; typedef vector<Array> Matrix; Matrix adjacency(const Graph &graph) { int n = graph.size(); Matrix distance(n, Array(n, 1e9)); for (const Edges &edges : graph) for (const Edge &edge : edges) { distance[edge.from][edge.to] = edge.weight; } return distance; } void debug(const Graph &g) { for (const Edges &es : g) for (const Edge &e : es) { cerr << e.from << " " << e.to << " " << e.weight << endl; } } int solve(const Graph &graph, int v, bitset<100> bit) { int res = 1; for (const auto &edge : graph[v]) if (!bit[edge.to]) { bit[edge.to] = true; res = max(res, solve(graph, edge.to, bit) + 1); bit[edge.to] = false; } return res; } int main() { while (true) { int n; cin >> n; if (n == 0) break; Graph graph(n); for (int i : range(n)) { int a, b; cin >> a >> b; --a, --b; graph[a].emplace_back(a, b, 1); graph[b].emplace_back(b, a, 1); } int res = 0; for (int i : range(n)) { bitset<100> bit; bit[i] = true; res = max(res, solve(graph, i, bit)); } cout << res << endl; } }
#include <bitset> #include <iostream> #include <vector> using namespace std; class range { private: struct Iterator { int val; int operator*() { return val; } bool operator!=(Iterator &itr) { return val < itr.val; } void operator++() { ++val; } }; Iterator i, n; public: range(int n) : i({0}), n({n}) {} range(int i, int n) : i({i}), n({n}) {} Iterator &begin() { return i; } Iterator &end() { return n; } }; template <class T> T at(vector<T> v, int i) { return v[(i % (int)v.size() + v.size()) % v.size()]; } typedef int Weight; struct Edge { int from, to; Weight weight; Edge(int from, int to, Weight weight) : from(from), to(to), weight(weight) {} }; typedef vector<Edge> Edges; typedef vector<Edges> Graph; typedef vector<Weight> Array; typedef vector<Array> Matrix; Matrix adjacency(const Graph &graph) { int n = graph.size(); Matrix distance(n, Array(n, 1e9)); for (const Edges &edges : graph) for (const Edge &edge : edges) { distance[edge.from][edge.to] = edge.weight; } return distance; } void debug(const Graph &g) { for (const Edges &es : g) for (const Edge &e : es) { cerr << e.from << " " << e.to << " " << e.weight << endl; } } int solve(const Graph &graph, int v, bitset<100> bit) { int res = 1; for (const auto &edge : graph[v]) if (!bit[edge.to]) { bit[edge.to] = true; res = max(res, solve(graph, edge.to, bit) + 1); bit[edge.to] = false; } return res; } int main() { while (true) { int n; cin >> n; if (n == 0) break; Graph graph(100); for (int i : range(n)) { int a, b; cin >> a >> b; --a, --b; graph[a].emplace_back(a, b, 1); graph[b].emplace_back(b, a, 1); } int res = 1; for (int i : range(100)) { bitset<100> bit; bit[i] = true; res = max(res, solve(graph, i, bit)); } cout << res << endl; } }
[["-", 0, 43, 49, 53, 54, 55, 0, 56, 39, 78], ["+", 8, 9, 0, 43, 49, 50, 51, 4, 0, 13], ["-", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 52, 8, 9, 0, 43, 49, 50, 51, 13], ["-", 8, 9, 0, 338, 12, 2, 3, 4, 0, 22], ["+", 8, 9, 0, 338, 12, 2, 3, 4, 0, 13]]
1
611
#include <cstdio> #include <queue> #include <vector> #define rep(i, n) for (int i = 0; i < n; i++) #define pb push_back using namespace std; vector<int> e[100]; bool used[100]; int ans; void dfs(int x, int y) { if (used[x]) { if (y > ans) ans = y; return; } used[x] = true; rep(i, e[x].size()) { // if(used[e[x][i]]) continue; dfs(e[x][i], y + 1); } used[x] = false; } int main() { int n; while (scanf("%d", &n), n) { rep(i, 100) e[i].clear(); rep(i, n) { int a, b; scanf("%d%d", &a, &b); a--; b--; e[a].pb(b); e[b].pb(a); } ans = 0; rep(i, n) { rep(j, 100) used[j] = false; dfs(i, 0); } printf("%d\n", ans); } }
#include <cstdio> #include <queue> #include <vector> #define rep(i, n) for (int i = 0; i < n; i++) #define pb push_back using namespace std; vector<int> e[100]; bool used[100]; int ans; void dfs(int x, int y) { if (used[x]) { if (y > ans) ans = y; return; } used[x] = true; rep(i, e[x].size()) { // if(used[e[x][i]]) continue; dfs(e[x][i], y + 1); } used[x] = false; } int main() { int n; while (scanf("%d", &n), n) { rep(i, 100) e[i].clear(); rep(i, n) { int a, b; scanf("%d%d", &a, &b); a--; b--; e[a].pb(b); e[b].pb(a); } ans = 0; rep(i, 100) { rep(j, 100) used[j] = false; dfs(i, 0); } printf("%d\n", ans); } }
[["-", 0, 14, 49, 53, 54, 55, 0, 56, 39, 78], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13], ["+", 0, 1, 0, 2, 3, 4, 0, 25, 0, 35]]
1
252
#include <bits/stdc++.h> using namespace std; #define PB push_back const int INF = 999; vector<int> v[100]; bool vis[100]; bool f[100]; int search(int now) { // vis[now] = true; int res = 1; for (int i = 0; i < v[now].size(); i++) { vis[now] = true; if (!vis[v[now][i]]) res = max(res, search(v[now][i]) + 1); vis[now] = false; } // vis[now] = false; return res; } int main() { int n; while (scanf("%d", &n), n) { for (int i = 0; i < 100; i++) { v[i].clear(); } memset(vis, 0, sizeof(vis)); memset(f, 0, sizeof(f)); for (int i = 0; i < n; i++) { int a, b; scanf("%d %d", &a, &b); a--; b--; v[a].PB(b); v[b].PB(a); f[a] = true; f[b] = true; } /* int mini = INF; for (int i = 0; i < n; i++){ mini = min(mini, (int)v[i].size()); } */ int maxi = 0; for (int i = 0; i < n; i++) { // if (v[i].size() - mini <= 1){ if (f[i]) { maxi = max(maxi, search(i)); } //} } printf("%d\n", maxi); } return 0; }
#include <bits/stdc++.h> using namespace std; #define PB push_back const int INF = 999; vector<int> v[100]; bool vis[100]; bool f[100]; int search(int now) { // vis[now] = true; int res = 1; for (int i = 0; i < v[now].size(); i++) { vis[now] = true; if (!vis[v[now][i]]) res = max(res, search(v[now][i]) + 1); vis[now] = false; } // vis[now] = false; return res; } int main() { int n; while (scanf("%d", &n), n) { for (int i = 0; i < 100; i++) { v[i].clear(); } memset(vis, 0, sizeof(vis)); memset(f, 0, sizeof(f)); for (int i = 0; i < n; i++) { int a, b; scanf("%d %d", &a, &b); a--; b--; v[a].PB(b); v[b].PB(a); f[a] = true; f[b] = true; } /* int mini = INF; for (int i = 0; i < n; i++){ mini = min(mini, (int)v[i].size()); } */ int maxi = 0; for (int i = 0; i < 100; i++) { // if (v[i].size() - mini <= 1){ if (f[i]) { maxi = max(maxi, search(i)); } //} } printf("%d\n", maxi); } return 0; }
[["-", 0, 52, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 12, 13]]
1
331
#include <algorithm> #include <cstdio> #include <vector> using namespace std; vector<int> es[100]; int max_length; bool used[100]; void get_max_length(int at_pos, int sum_length) { used[at_pos] = true; for (int i = 0; i < es[at_pos].size(); i++) { int next_pos = es[at_pos][i]; if (used[next_pos]) continue; get_max_length(next_pos, sum_length + 1); } max_length = max(max_length, sum_length); used[at_pos] = false; } int main() { int n; while (scanf("%d", &n), n) { for (int i = 0; i < n; i++) es[i].clear(); max_length = 0; for (int i = 0; i < n; i++) { int a, b; scanf("%d%d", &a, &b); a--; b--; es[a].push_back(b); es[b].push_back(a); } for (int i = 0; i < n; i++) get_max_length(i, 1); printf("%d\n", max_length); } return 0; }
#include <algorithm> #include <cstdio> #include <vector> using namespace std; vector<int> es[100]; int max_length; bool used[100]; void get_max_length(int at_pos, int sum_length) { used[at_pos] = true; for (int i = 0; i < es[at_pos].size(); i++) { int next_pos = es[at_pos][i]; if (used[next_pos]) continue; get_max_length(next_pos, sum_length + 1); } max_length = max(max_length, sum_length); used[at_pos] = false; } int main() { int n; while (scanf("%d", &n), n) { for (int i = 0; i < 100; i++) es[i].clear(); max_length = 0; for (int i = 0; i < n; i++) { int a, b; scanf("%d%d", &a, &b); a--; b--; es[a].push_back(b); es[b].push_back(a); } for (int i = 0; i < 100; i++) get_max_length(i, 1); printf("%d\n", max_length); } return 0; }
[["-", 0, 52, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 12, 13]]
1
260
#include <algorithm> #include <iostream> #include <vector> using namespace std; bool isVisited[101]; int dfs(const vector<vector<int>> &r, int pos) { if (isVisited[pos]) { return 0; } isVisited[pos] = true; int ret = 0; for (int i = 0; i < r[pos].size(); i++) { ret = max(ret, dfs(r, r[pos][i])); } isVisited[pos] = false; return ret + 1; } int main(void) { for (int i = 0; i <= 100; i++) { isVisited[i] = false; } int n; while (cin >> n, n) { vector<vector<int>> rings(n + 1); int a, b; for (int i = 0; i < n; i++) { cin >> a >> b; rings[a].push_back(b); rings[b].push_back(a); } int ans = 0; for (int i = 1; i <= n; i++) { ans = max(ans, dfs(rings, i)); } cout << ans << endl; } return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; bool isVisited[101]; int dfs(const vector<vector<int>> &r, int pos) { if (isVisited[pos]) { return 0; } isVisited[pos] = true; int ret = 0; for (int i = 0; i < r[pos].size(); i++) { ret = max(ret, dfs(r, r[pos][i])); } isVisited[pos] = false; return ret + 1; } int main(void) { for (int i = 0; i <= 100; i++) { isVisited[i] = false; } int n; while (cin >> n, n) { vector<vector<int>> rings(101); int a, b; for (int i = 0; i < n; i++) { cin >> a >> b; rings[a].push_back(b); rings[b].push_back(a); } int ans = 0; for (int i = 1; i <= 100; i++) { ans = max(ans, dfs(rings, i)); } cout << ans << endl; } return 0; }
[["-", 0, 43, 49, 50, 51, 4, 0, 16, 31, 22], ["-", 0, 43, 49, 50, 51, 4, 0, 16, 17, 72], ["-", 0, 43, 49, 50, 51, 4, 0, 16, 12, 13], ["+", 8, 9, 0, 43, 49, 50, 51, 4, 0, 13], ["-", 0, 52, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 12, 13]]
1
262
#include <algorithm> #include <stdio.h> using namespace std; int N, A, B, v[111], r[111], G[111][222]; int rec(int pos) { int ret = 0; v[pos] = 1; for (int i = 0; i < r[pos]; i++) { if (!v[G[pos][i]]) ret = max(ret, rec(G[pos][i])); } v[pos] = 0; return ret; } int main() { while (scanf("%d", &N), N) { for (int i = 0; i < 100; i++) r[i] = 0; for (int i = 0; i < N; i++) { scanf("%d%d", &A, &B), A--, B--; G[A][r[A]++] = B, G[B][r[B]++] = A; } int ret = 0; for (int i = 0; i < 100; i++) ret = max(ret, rec(i)); printf("%d\n", ret + 1); } return 0; }
#include <algorithm> #include <stdio.h> using namespace std; int N, A, B, v[111], r[111], G[111][222]; int rec(int pos) { int ret = 0; v[pos] = 1; for (int i = 0; i < r[pos]; i++) { if (!v[G[pos][i]]) ret = max(ret, rec(G[pos][i])); } v[pos] = 0; return ret + 1; } int main() { while (scanf("%d", &N), N) { for (int i = 0; i < 100; i++) r[i] = 0; for (int i = 0; i < N; i++) { scanf("%d%d", &A, &B), A--, B--; G[A][r[A]++] = B, G[B][r[B]++] = A; } int ret = 0; for (int i = 0; i < 100; i++) ret = max(ret, rec(i)); printf("%d\n", ret); } return 0; }
[["+", 0, 14, 8, 9, 0, 37, 0, 16, 17, 72], ["+", 0, 14, 8, 9, 0, 37, 0, 16, 12, 13], ["-", 0, 1, 0, 2, 3, 4, 0, 16, 17, 72], ["-", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13]]
1
266
#include <iostream> #include <vector> using namespace std; int n; vector<int> r[100]; bool ud[100]; int func(int a) { int l = 1; ud[a] = true; for (int j = 0; j < r[a].size(); ++j) { if (!ud[r[a][j]]) { l = max(l, func(r[a][j]) + 1); } } ud[a] = false; return l; } int main() { int f, t, a; while (cin >> n, n) { a = 0; for (int i = 0; i < n; ++i) { ud[i] = false; r[i].clear(); } for (int i = 0; i < n; ++i) { cin >> f >> t; r[f - 1].push_back(t - 1); r[t - 1].push_back(f - 1); } for (int i = 0; i < n; ++i) a = max(a, func(i)); cout << a << endl; } return 0; }
#include <iostream> #include <vector> using namespace std; int n; vector<int> r[100]; bool ud[100]; int func(int a) { int l = 1; ud[a] = true; for (int j = 0; j < r[a].size(); ++j) { if (!ud[r[a][j]]) { l = max(l, func(r[a][j]) + 1); } } ud[a] = false; return l; } int main() { int f, t; while (cin >> n, n) { int a = 0; for (int i = 0; i < 100; ++i) { ud[i] = false; r[i].clear(); } for (int i = 0; i < n; ++i) { cin >> f >> t; r[f - 1].push_back(t - 1); r[t - 1].push_back(f - 1); } for (int i = 0; i < 100; ++i) a = max(a, func(i)); cout << a << endl; } return 0; }
[["-", 0, 30, 0, 14, 8, 9, 0, 43, 0, 21], ["-", 0, 30, 0, 14, 8, 9, 0, 43, 49, 22], ["+", 8, 9, 0, 52, 8, 9, 0, 43, 39, 40], ["-", 0, 52, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 12, 13]]
1
259
#include <bits/stdc++.h> using namespace std; vector<int> G[101]; bool V[101] = {}; int N; int Ans = 0; void DFS(int n, int len) { Ans = max(Ans, len); bool Vis = false; int S = G[n].size(); for (int i = 0; i < S; i++) { if (!V[G[n][i]]) { Vis = true; V[G[n][i]] = true; DFS(G[n][i], len + 1); V[G[n][i]] = false; } } return; } int main() { do { for (int i = 0; i < 101; i++) { G[i].clear(); V[i] = false; } Ans = 0; scanf("%d", &N); if (N == 0) break; for (int i = 0; i < N; i++) { int A, B; scanf("%d%d", &A, &B); G[A].push_back(B); G[B].push_back(A); } for (int i = 1; i <= N; i++) { V[i] = true; DFS(i, 1); V[i] = false; } printf("%d\n", Ans); } while (true); }
#include <bits/stdc++.h> using namespace std; vector<int> G[101]; bool V[101] = {}; int N; int Ans = 0; void DFS(int n, int len) { Ans = max(Ans, len); bool Vis = false; int S = G[n].size(); for (int i = 0; i < S; i++) { if (!V[G[n][i]]) { Vis = true; V[G[n][i]] = true; DFS(G[n][i], len + 1); V[G[n][i]] = false; } } return; } int main() { do { for (int i = 0; i < 101; i++) { G[i].clear(); V[i] = false; } Ans = 0; scanf("%d", &N); if (N == 0) break; for (int i = 0; i < N; i++) { int A, B; scanf("%d%d", &A, &B); G[A].push_back(B); G[B].push_back(A); } for (int i = 1; i <= 100; i++) { V[i] = true; DFS(i, 1); V[i] = false; } printf("%d\n", Ans); } while (true); }
[["-", 0, 82, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 82, 8, 9, 0, 7, 15, 16, 12, 13]]
1
317
#include <algorithm> #include <cstdio> #include <cstring> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <unordered_map> #include <vector> using namespace std; vector<int> rinsetu[100]; bool S[100]; int MAX; void saiki(int a, int b) { MAX = max(MAX, b); for (int i : rinsetu[a]) { if (!S[i]) { S[i] = true; saiki(i, b + 1); S[i] = false; } } } signed main() { int a; while (cin >> a, a) { MAX = 0; for (int i = 0; i < 100; i++) rinsetu[i].clear(); memset(S, false, sizeof(S)); for (int b = 0; b < a; b++) { int c, d; scanf("%d%d", &c, &d); c--; d--; rinsetu[c].push_back(d); rinsetu[d].push_back(c); } for (int b = 0; b < a; b++) { S[b] = true; saiki(b, 1); S[b] = false; } cout << MAX << endl; } }
#include <algorithm> #include <cstdio> #include <cstring> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <unordered_map> #include <vector> using namespace std; vector<int> rinsetu[105]; bool S[105]; int MAX; void saiki(int a, int b) { MAX = max(MAX, b); for (int i : rinsetu[a]) { if (!S[i]) { S[i] = true; saiki(i, b + 1); S[i] = false; } } } signed main() { int a; while (cin >> a, a) { MAX = 0; for (int i = 0; i < 105; i++) rinsetu[i].clear(); memset(S, false, sizeof(S)); for (int b = 0; b < a; b++) { int c, d; scanf("%d%d", &c, &d); c--; d--; rinsetu[c].push_back(d); rinsetu[d].push_back(c); } for (int b = 0; b < 105; b++) { S[b] = true; saiki(b, 1); S[b] = false; } cout << MAX << endl; } }
[["-", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13], ["+", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13], ["-", 0, 52, 8, 9, 0, 7, 15, 16, 12, 13], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 12, 13], ["-", 0, 52, 8, 9, 0, 7, 15, 16, 12, 22]]
1
270
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define INF 1 << 30 #define LINF 1LL << 60 /* <url:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0508> ?????????============================================================ ?????????????????°?????????????´?????????????.????????°?????£??´??°??????????????????,?´?????????\??????. ?´?????????????????????°????????°????????° a, b ??????????????????.?????????,[a, b] ??¨?¨???°??????. ?????°????´??????????,??????????´???¨??????????´????????????°????????????????????°???????????´???,??????????????°?????¨????????§, ?????????????´?????????????????????¨?????§??????,??????????????§????????????????????¨?????¶?????¨?????????. ????????°,[1, 3] ??¨ [3, 4] ??¨???????´?????????? [1, 3, 4] ??¨??????????????§??????. ?´???¨??????????????????????????´??°??????????????????????????°?????¨????????§?????????????????¨?????§??????????????¨??????. ????????°,??? [1, 3, 4] ??¨?´? [5, 1] ????????? [5, 1, 3, 4] ?????§???, ??? [1, 3, 4] ??¨??? [2, 3, 5] ?????????, ????????§??????????????????????????¢?????????. ??? [1, 3, 4] ??¨??? [4, 6, 1] ?????????, ????????¢?????§??????. ????????????????§?????????¢?????§?????????, ????????????????????¨??§, ????????°????????????????????°???????????????????????????????????£????????°????´?????????¨?????¶?????¨?????????. ????????°,??? [1, 3, 4] ??¨???[2, 3, 5] ????????§??????,????????§??????????????????????????¢??????, [1, 3, 5],[2, 3, 4] ??¨?????????????????????, ??? [1, 3, 4] ??¨??? [4, 6, 1] ????????§???????????????, [1, 3, 4, 6],[3, 4, 6, 1],[4, 6, 1, 3]???????????????????????????. ???????????????????????????,??????????????°????????°???????????¨????????????. ????????????????????°????´?????????????,??????????????????????????????????????????????????¨1?????\????????????????????¢?????§??????. ????????????????????§???????????????????±???????????????°?????????????????????. ??\???????????? ??????????????????????´?????????°??§????????£??´??° 1 ??? n ??? 100 ??????????????????, ?????\??? n ????????????????????????, ????????§??????????????? 2 ????????´??° a, b ?????????????????? 1 ??? a < b ??? 100 ??¨?????£?????????. ????????? 2 ????????´??°??? 1 ???????´???????????????´??°?????¨??????. ================================================================= ??\???============================================================= ??\???????????°????????????????????????????????????n ??? 0 ?????¨?????\????????????????????????????????????????????°??? 10 ????¶????????????? ================================================================ ??????============================================================= ???????????????????????¨???????????§?????????????????????????????????????????? ================================================================ ?§£??¬============================================================= ================================================================ */ ll ans; void dfs(ll n, ll dep, vector<vector<ll>> &G, vector<int> &used) { bool f = false; for (auto next : G[n]) { if (used[next] == 0) { f = true; used[next] = 1; dfs(next, dep + 1, G, used); used[next] = 0; } } if (!f) { ans = max(ans, dep); } } int main(void) { cin.tie(0); ios::sync_with_stdio(false); ll n; while (cin >> n, n) { ans = 0; vector<vector<ll>> G(n); for (int i = 0; i < n; i++) { ll x, y; cin >> x >> y; x--; y--; G[x].push_back(y); G[y].push_back(x); vector<int> used(n, 0); for (int i = 0; i < n; i++) { used[i] = 1; dfs(i, 1, G, used); used[i] = 0; } } cout << ans << endl; } return 0; }
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define INF 1 << 30 #define LINF 1LL << 60 /* <url:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0508> ?????????============================================================ ?????????????????°?????????????´?????????????.????????°?????£??´??°??????????????????,?´?????????\??????. ?´?????????????????????°????????°????????° a, b ??????????????????.?????????,[a, b] ??¨?¨???°??????. ?????°????´??????????,??????????´???¨??????????´????????????°????????????????????°???????????´???,??????????????°?????¨????????§, ?????????????´?????????????????????¨?????§??????,??????????????§????????????????????¨?????¶?????¨?????????. ????????°,[1, 3] ??¨ [3, 4] ??¨???????´?????????? [1, 3, 4] ??¨??????????????§??????. ?´???¨??????????????????????????´??°??????????????????????????°?????¨????????§?????????????????¨?????§??????????????¨??????. ????????°,??? [1, 3, 4] ??¨?´? [5, 1] ????????? [5, 1, 3, 4] ?????§???, ??? [1, 3, 4] ??¨??? [2, 3, 5] ?????????, ????????§??????????????????????????¢?????????. ??? [1, 3, 4] ??¨??? [4, 6, 1] ?????????, ????????¢?????§??????. ????????????????§?????????¢?????§?????????, ????????????????????¨??§, ????????°????????????????????°???????????????????????????????????£????????°????´?????????¨?????¶?????¨?????????. ????????°,??? [1, 3, 4] ??¨???[2, 3, 5] ????????§??????,????????§??????????????????????????¢??????, [1, 3, 5],[2, 3, 4] ??¨?????????????????????, ??? [1, 3, 4] ??¨??? [4, 6, 1] ????????§???????????????, [1, 3, 4, 6],[3, 4, 6, 1],[4, 6, 1, 3]???????????????????????????. ???????????????????????????,??????????????°????????°???????????¨????????????. ????????????????????°????´?????????????,??????????????????????????????????????????????????¨1?????\????????????????????¢?????§??????. ????????????????????§???????????????????±???????????????°?????????????????????. ??\???????????? ??????????????????????´?????????°??§????????£??´??° 1 ??? n ??? 100 ??????????????????, ?????\??? n ????????????????????????, ????????§??????????????? 2 ????????´??° a, b ?????????????????? 1 ??? a < b ??? 100 ??¨?????£?????????. ????????? 2 ????????´??°??? 1 ???????´???????????????´??°?????¨??????. ================================================================= ??\???============================================================= ??\???????????°????????????????????????????????????n ??? 0 ?????¨?????\????????????????????????????????????????????°??? 10 ????¶????????????? ================================================================ ??????============================================================= ???????????????????????¨???????????§?????????????????????????????????????????? ================================================================ ?§£??¬============================================================= ================================================================ */ ll ans; void dfs(ll n, ll dep, vector<vector<ll>> &G, vector<int> &used) { bool f = false; for (auto next : G[n]) { if (used[next] == 0) { f = true; used[next] = 1; dfs(next, dep + 1, G, used); used[next] = 0; } } if (!f) { ans = max(ans, dep); } } int main(void) { cin.tie(0); ios::sync_with_stdio(false); ll n; while (cin >> n, n) { ans = 0; vector<vector<ll>> G(101); for (int i = 0; i < n; i++) { ll x, y; cin >> x >> y; x--; y--; G[x].push_back(y); G[y].push_back(x); } vector<int> used(101, 0); for (int i = 0; i < 101; i++) { used[i] = 1; dfs(i, 1, G, used); used[i] = 0; } cout << ans << endl; } return 0; }
[["-", 0, 43, 49, 53, 54, 55, 0, 56, 39, 78], ["+", 8, 9, 0, 43, 49, 50, 51, 4, 0, 13], ["+", 0, 52, 8, 9, 0, 7, 8, 9, 0, 46], ["-", 8, 9, 0, 43, 49, 50, 51, 4, 0, 22], ["-", 0, 7, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 12, 13], ["-", 0, 7, 8, 9, 0, 7, 8, 9, 0, 46]]
1
308
#include <cstring> #include <iostream> #include <set> #include <vector> using namespace std; // int node[101][101]; int visit[101]; int ans, last; int die[101]; int flag; void dfs(int now, int cnt, int *st, vector<vector<int>> &vv) { if (cnt > ans) { ans = cnt; last = now; flag = 1; } for (int next = 0; next < vv[now].size(); next++) if (!visit[vv[now][next]]) { int k = vv[now][next]; visit[k] = die[k] = 1; st[cnt] = k; dfs(k, cnt + 1, st, vv); visit[k] = 0; } return; } int main() { int n; while (cin >> n, n) { set<int> s; vector<vector<int>> vv(n + 1); ans = 0; // memset(node,0,sizeof(node)); memset(visit, 0, sizeof(visit)); memset(die, 0, sizeof(die)); for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; // node[a][b] = node[b][a] = 1; vv[a].push_back(b); vv[b].push_back(a); s.insert(a); s.insert(b); } int tmp[101]; for (set<int>::iterator it = s.begin(); it != s.end(); it++) if (!die[*it]) { flag = 0; visit[*it] = 1; tmp[0] = *it; dfs(*it, 1, tmp, vv); visit[*it] = 0; die[last] = 0; // printf("*it:%d\n",*it); } printf("%d\n", ans); } return 0; }
#include <cstring> #include <iostream> #include <set> #include <vector> using namespace std; // int node[101][101]; int visit[101]; int ans, last; int die[101]; int flag; void dfs(int now, int cnt, int *st, vector<vector<int>> &vv) { if (cnt > ans) { ans = cnt; last = now; flag = 1; } for (int next = 0; next < vv[now].size(); next++) if (!visit[vv[now][next]]) { int k = vv[now][next]; visit[k] = die[k] = 1; st[cnt] = k; dfs(k, cnt + 1, st, vv); visit[k] = 0; } return; } int main() { int n; while (cin >> n, n) { set<int> s; vector<vector<int>> vv(101); ans = 0; // memset(node,0,sizeof(node)); memset(visit, 0, sizeof(visit)); memset(die, 0, sizeof(die)); for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; // node[a][b] = node[b][a] = 1; vv[a].push_back(b); vv[b].push_back(a); s.insert(a); s.insert(b); } int tmp[101]; /* for(int i=0; i<vv.size(); i++){ printf("vv[%d].size = %d\n",i,vv[i].size()); for(int j=0; j<vv[i].size(); j++) printf("%d ",vv[i][j]); puts(""); } */ for (set<int>::iterator it = s.begin(); it != s.end(); it++) if (!die[*it]) { flag = 0; visit[*it] = 1; tmp[0] = *it; dfs(*it, 1, tmp, vv); visit[*it] = 0; die[last] = 0; // printf("*it:%d\n",*it); } printf("%d\n", ans); } return 0; }
[["-", 0, 43, 49, 50, 51, 4, 0, 16, 31, 22], ["-", 0, 43, 49, 50, 51, 4, 0, 16, 17, 72], ["-", 0, 43, 49, 50, 51, 4, 0, 16, 12, 13], ["+", 8, 9, 0, 43, 49, 50, 51, 4, 0, 13]]
1
402
#include <algorithm> #include <iostream> #include <vector> using namespace std; int result; vector<int> strands[100]; bool used[100]; void rec(int k, int d, int ub) { if (result < d) result = d; used[k] = 1; for (int i = 0; i < strands[k].size(); i++) if (!used[strands[k][i]]) ub--; ub++; if (result < ub) for (int i = 0; i < strands[k].size(); i++) { int j = strands[k][i]; if (!used[j]) rec(j, d + 1, ub); } used[k] = 0; } int main() { for (;;) { int n; cin >> n; if (!n) return 0; for (int i = 0; i < n; i++) strands[i].clear(); fill(used, used + 100, false); for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; a--, b--; strands[a].push_back(b); strands[b].push_back(a); } result = 0; for (int i = 0; i < n; i++) rec(i, 1, n + 1); cout << result << endl; } }
#include <algorithm> #include <iostream> #include <vector> using namespace std; int result; vector<int> strands[100]; bool used[100]; void rec(int k, int d, int ub) { if (result < d) result = d; used[k] = 1; for (int i = 0; i < strands[k].size(); i++) if (!used[strands[k][i]]) ub--; ub++; if (result < ub) for (int i = 0; i < strands[k].size(); i++) { int j = strands[k][i]; if (!used[j]) rec(j, d + 1, ub); } used[k] = 0; } int main() { for (;;) { int n; cin >> n; if (!n) return 0; for (int i = 0; i < 100; i++) strands[i].clear(); fill(used, used + 100, false); for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; a--, b--; strands[a].push_back(b); strands[b].push_back(a); } result = 0; for (int i = 0; i < 100; i++) rec(i, 1, n + 1); cout << result << endl; } }
[["-", 0, 7, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 12, 13]]
1
316
#include <cstdio> #include <cstring> #include <iostream> #include <vector> using namespace std; int n; vector<int> G[101]; int ans = 0; int mn[101]; bool vis[101]; bool nuse[101]; int INF = 10000; vector<int> group; int dfs(int v) { vis[v] = true; int res = 0; for (int i = 0; i < G[v].size(); i++) { if (vis[G[v][i]]) continue; vis[G[v][i]] = true; res = max(res, dfs(G[v][i])); vis[G[v][i]] = false; } return res + 1; } int searchmin(int v) { int res = G[v].size(); vis[v] = true; group.push_back(v); for (int i = 0; i < G[v].size(); i++) { if (!vis[G[v][i]]) res = min(res, searchmin(G[v][i])); } return res; } int main(void) { while (1) { for (int i = 0; i < 101; i++) G[i].clear(), mn[i] = INF; memset(nuse, false, sizeof(nuse)); ans = 0; scanf("%d", &n); if (n == 0) break; for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; G[a - 1].push_back(b - 1); G[b - 1].push_back(a - 1); } for (int i = 0; i < n; i++) { if (mn[i] != INF) continue; group.clear(); memset(vis, false, sizeof(vis)); int mini = searchmin(i); for (int j = 0; j < group.size(); j++) mn[group[j]] = mini; } for (int i = 0; i < n; i++) { if (G[i].size() == mn[i]) { memset(vis, false, sizeof(vis)); ans = max(ans, dfs(i)); } } cout << ans << endl; } return 0; }
#include <cstdio> #include <cstring> #include <iostream> #include <vector> using namespace std; int n; vector<int> G[101]; int ans = 0; int mn[101]; bool vis[101]; bool nuse[101]; int INF = 10000; vector<int> group; int dfs(int v) { vis[v] = true; int res = 0; for (int i = 0; i < G[v].size(); i++) { if (vis[G[v][i]]) continue; vis[G[v][i]] = true; res = max(res, dfs(G[v][i])); vis[G[v][i]] = false; } return res + 1; } int searchmin(int v) { int res = G[v].size(); vis[v] = true; group.push_back(v); for (int i = 0; i < G[v].size(); i++) { if (!vis[G[v][i]]) res = min(res, searchmin(G[v][i])); } return res; } int main(void) { while (1) { for (int i = 0; i < 101; i++) G[i].clear(), mn[i] = INF; memset(nuse, false, sizeof(nuse)); ans = 0; scanf("%d", &n); if (n == 0) break; for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; G[a - 1].push_back(b - 1); G[b - 1].push_back(a - 1); } for (int i = 0; i < 101; i++) { if (mn[i] != INF) continue; group.clear(); memset(vis, false, sizeof(vis)); int mini = searchmin(i); for (int j = 0; j < group.size(); j++) mn[group[j]] = mini; } for (int i = 0; i < 101; i++) { if (G[i].size() == mn[i]) { memset(vis, false, sizeof(vis)); ans = max(ans, dfs(i)); } } cout << ans << endl; } return 0; }
[["-", 0, 52, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 12, 13]]
1
537
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <utility> #include <vector> using namespace std; int N; vector<vector<int>> g; int ans; void dfs(int cur, bool *visited, int len) { for (int n : g[cur]) { if (visited[n]) continue; visited[n] = true; dfs(n, visited, len + 1); visited[n] = false; } ans = max(ans, len); } int main() { bool visited[100] = {}; while (true) { cin >> N; if (N == 0) break; g.clear(); g.resize(100); ans = 0; for (int i = 0; i < N; ++i) { int a, b; cin >> a >> b; --a; --b; if (find(g[a].begin(), g[a].end(), b) == g[a].end()) g[a].push_back(b); if (find(g[b].begin(), g[b].end(), a) == g[b].end()) g[b].push_back(a); } for (int i = 0; i < 1; ++i) { visited[i] = true; dfs(i, visited, 1); visited[i] = false; } cout << ans << endl; } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <utility> #include <vector> using namespace std; int N; vector<vector<int>> g; int ans; void dfs(int cur, bool *visited, int len) { for (int n : g[cur]) { if (visited[n]) continue; visited[n] = true; dfs(n, visited, len + 1); visited[n] = false; } ans = max(ans, len); } int main() { bool visited[100] = {}; while (true) { cin >> N; if (N == 0) break; g.clear(); g.resize(100); ans = 0; for (int i = 0; i < N; ++i) { int a, b; cin >> a >> b; --a; --b; if (find(g[a].begin(), g[a].end(), b) == g[a].end()) g[a].push_back(b); if (find(g[b].begin(), g[b].end(), a) == g[b].end()) g[b].push_back(a); } for (int i = 0; i < 100; ++i) { visited[i] = true; dfs(i, visited, 1); visited[i] = false; } cout << ans << endl; } return 0; }
[["-", 0, 52, 8, 9, 0, 7, 15, 16, 12, 13], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 12, 13]]
1
320
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i) #define REP(i, n) FOR(i, 0, n) #define FORIT(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) template <class T> void debug(T begin, T end) { for (T i = begin; i != end; ++i) cerr << *i << " "; cerr << endl; } inline bool valid(int x, int y, int W, int H) { return (x >= 0 && y >= 0 && x < W && y < H); } typedef long long ll; const int INF = 100000000; const double EPS = 1e-8; const int MOD = 1000000007; int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; struct Edge { int src, dst; Edge() {} Edge(int u, int v) : src(u), dst(v) {} }; typedef vector<Edge> Node; typedef vector<Node> Graph; bool used[100]; int dfs(int u, Graph &G) { used[u] = true; int res = 0; bool tmp_used[100]; memcpy(tmp_used, used, sizeof(used)); FORIT(e, G[u]) if (!used[e->dst]) { res = max(res, 1 + dfs(e->dst, G)); memcpy(used, tmp_used, sizeof(used)); } return res; } int main() { int N; const int M = 100; while (cin >> N && N) { Graph G(M); REP(i, N) { int u, v; cin >> u >> v; u--; v--; G[u].push_back(Edge(u, v)); G[v].push_back(Edge(v, u)); } int ans = 0; REP(root, N) { memset(used, 0, sizeof(used)); ans = max(ans, dfs(root, G) + 1); } cout << ans << endl; } return 0; }
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i) #define REP(i, n) FOR(i, 0, n) #define FORIT(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) template <class T> void debug(T begin, T end) { for (T i = begin; i != end; ++i) cerr << *i << " "; cerr << endl; } inline bool valid(int x, int y, int W, int H) { return (x >= 0 && y >= 0 && x < W && y < H); } typedef long long ll; const int INF = 100000000; const double EPS = 1e-8; const int MOD = 1000000007; int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; struct Edge { int src, dst; Edge() {} Edge(int u, int v) : src(u), dst(v) {} }; typedef vector<Edge> Node; typedef vector<Node> Graph; bool used[100]; int dfs(int u, Graph &G) { used[u] = true; int res = 0; bool tmp_used[100]; memcpy(tmp_used, used, sizeof(used)); FORIT(e, G[u]) if (!used[e->dst]) { res = max(res, 1 + dfs(e->dst, G)); memcpy(used, tmp_used, sizeof(used)); } return res; } int main() { int N; const int M = 100; while (cin >> N && N) { Graph G(M); REP(i, N) { int u, v; cin >> u >> v; u--; v--; G[u].push_back(Edge(u, v)); G[v].push_back(Edge(v, u)); } int ans = 0; REP(root, M) { memset(used, 0, sizeof(used)); ans = max(ans, dfs(root, G) + 1); } cout << ans << endl; } return 0; }
[["-", 0, 14, 49, 53, 54, 55, 0, 56, 39, 78], ["+", 0, 14, 49, 53, 54, 55, 0, 56, 39, 78]]
1
498
#include <bits/stdc++.h> using namespace std; bool mp[10005][4]; int main() { ios_base::sync_with_stdio(false); int h, n, x, y; for (int i = 0; i < 10005; i++) { for (int j = 0; j < 4; j++) { mp[i][j] = true; } } cin >> h >> n; for (int i = 0; i < n; i++) { cin >> x >> y; mp[y][x] = false; } int ans = 0; for (int i = 0; i < h - 1; i++) { if (mp[i][0] && mp[i][1] && mp[i + 1][0] && mp[i + 1][1]) { //左に正方形が作れるとき mp[i][0] = mp[i][1] = mp[i + 1][0] = mp[i + 1][1] = false; ans++; } if (mp[i][2] && mp[i][3] && mp[i + 1][2] && mp[i + 1][3]) { //右に正方形が作れるとき mp[i][2] = mp[i][3] = mp[i + 1][2] = mp[i + 1][3] = false; ans++; } if (mp[i][1] && mp[i][2] && mp[i + 1][1] && mp[i + 1][2]) { //真ん中に正方形が作れるとき int j = 0; while (i + 1 + j < h && mp[i + 1 + j][0] && mp[i + 1 + j][1] && mp[i + 1 + j][2] && mp[i + 1 + j][3]) j++; if (j != 0 && j % 2 == 1) { mp[i][1] = mp[i][2] = mp[i + 1][1] = mp[i + 1][2] = false; ans++; } } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; bool mp[10005][4]; int main() { ios_base::sync_with_stdio(false); int h, n, x, y; for (int i = 0; i < 10005; i++) { for (int j = 0; j < 4; j++) { mp[i][j] = true; } } cin >> h >> n; for (int i = 0; i < n; i++) { cin >> x >> y; mp[y][x] = false; } int ans = 0; for (int i = 0; i < h - 1; i++) { if (mp[i][0] && mp[i][1] && mp[i + 1][0] && mp[i + 1][1]) { //左に正方形が作れるとき mp[i][0] = mp[i][1] = mp[i + 1][0] = mp[i + 1][1] = false; ans++; } if (mp[i][2] && mp[i][3] && mp[i + 1][2] && mp[i + 1][3]) { //右に正方形が作れるとき mp[i][2] = mp[i][3] = mp[i + 1][2] = mp[i + 1][3] = false; ans++; } if (mp[i][1] && mp[i][2] && mp[i + 1][1] && mp[i + 1][2]) { //真ん中に正方形が作れるとき int j = 0; while (i + 1 + j < h && mp[i + 1 + j][0] && mp[i + 1 + j][1] && mp[i + 1 + j][2] && mp[i + 1 + j][3]) j++; if (j == 0 || j % 2 == 1) { mp[i][1] = mp[i][2] = mp[i + 1][1] = mp[i + 1][2] = false; ans++; } } } cout << ans << endl; return 0; }
[["-", 0, 57, 15, 339, 51, 16, 31, 16, 17, 79], ["+", 0, 57, 15, 339, 51, 16, 31, 16, 17, 60], ["-", 64, 9, 0, 57, 15, 339, 51, 16, 17, 98], ["+", 64, 9, 0, 57, 15, 339, 51, 16, 17, 106]]
1
475
#include <stdio.h> int main(void) { int w, h, n, x[100000], y[100000], Xmax, cnt, flg[100000], kyori, ymax, i; scanf("%d%d%d", &w, &h, &n); Xmax = 0; for (i = 1; i <= n; i++) { scanf("%d%d", &x[i], &y[i]); if (Xmax < x[i]) Xmax = x[i]; } for (i = 1; i <= Xmax; i++) flg[i] = -1; flg[0] = 0; for (i = 1; i <= n; i++) { if (flg[x[i]] < y[i]) flg[x[i]] = y[i]; } // for(i=1;i<=Xmax;i++) printf("flg[%d]=%d\n",i,flg[i]); kyori = 1000000; ymax = 0; for (i = Xmax; i > 0; i--) { if (flg[i] != -1) { if (kyori > ymax + i) kyori = ymax + i; if (ymax < flg[i]) ymax = flg[i]; } // printf("i=%d ymax=%d\n",i,ymax); } printf("%d\n", kyori); return 0; }
#include <stdio.h> int main(void) { int w, h, n, x[100000], y[100000], Xmax, cnt, flg[100000], kyori, ymax, i; scanf("%d%d%d", &w, &h, &n); Xmax = 0; for (i = 0; i < n; i++) { scanf("%d%d", &x[i], &y[i]); if (Xmax < x[i]) Xmax = x[i]; } for (i = 0; i <= Xmax; i++) flg[i] = -1; flg[0] = 0; for (i = 0; i < n; i++) { if (flg[x[i]] < y[i]) flg[x[i]] = y[i]; } // for(i=0;i<=Xmax;i++) printf("flg[%d]=%d\n",i,flg[i]); kyori = 1000000; ymax = 0; for (i = Xmax; i >= 0; i--) { if (flg[i] != -1) { if (kyori > ymax + i) kyori = ymax + i; if (ymax < flg[i]) ymax = flg[i]; } // printf("i=%d ymax=%d\n",i,ymax); } printf("%d\n", kyori); return 0; }
[["-", 0, 14, 8, 9, 0, 7, 10, 11, 12, 13], ["+", 0, 14, 8, 9, 0, 7, 10, 11, 12, 13], ["-", 0, 14, 8, 9, 0, 7, 15, 16, 17, 19], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 17, 18], ["-", 0, 14, 8, 9, 0, 7, 15, 16, 17, 47], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 17, 20]]
1
260
#include <bits/stdc++.h> #define r(i, n) for (int i = 0; i < n; i++) using namespace std; int w, n, m[100009], ans = 1e8, x, y; main() { cin >> w >> n >> n; r(i, n) { cin >> x >> y; m[x] = max(m[x], y); } r(i, n) m[n - i - 1] = max(m[n - i - 1], m[n - i]); r(i, w - 2) ans = min(ans, i + m[i + 1]); cout << ans << endl; }
#include <bits/stdc++.h> #define r(i, n) for (int i = 0; i < n; i++) using namespace std; int w, n, m[100009], ans = 1e8, x, y; main() { cin >> w >> n >> n; r(i, n) { cin >> x >> y; m[x] = max(m[x], y); } r(i, w) m[w - i - 1] = max(m[w - i - 1], m[w - i]); r(i, w) ans = min(ans, i + m[i + 1]); cout << ans << endl; }
[["-", 0, 11, 31, 69, 28, 2, 3, 4, 0, 22], ["+", 0, 11, 31, 69, 28, 2, 3, 4, 0, 22], ["-", 31, 69, 341, 342, 0, 16, 31, 16, 31, 22], ["+", 31, 69, 341, 342, 0, 16, 31, 16, 31, 22], ["-", 0, 69, 341, 342, 0, 16, 31, 16, 31, 22], ["+", 0, 69, 341, 342, 0, 16, 31, 16, 31, 22], ["-", 3, 4, 0, 69, 341, 342, 0, 16, 31, 22], ["+", 3, 4, 0, 69, 341, 342, 0, 16, 31, 22], ["-", 0, 11, 31, 2, 3, 4, 0, 16, 17, 33], ["-", 0, 11, 31, 2, 3, 4, 0, 16, 12, 13]]
1
138
#include <algorithm> #include <cstdlib> #include <ctime> #include <functional> #include <iostream> #include <limits.h> #include <list> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string.h> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #define P pair<int, int> using namespace std; vector<P> V; signed main() { int a, b, n; cin >> a >> b >> n; for (int i = 0; i < n; i++) { int c, d; cin >> c >> d; V.push_back({c, d}); } V.push_back({0, 0}); sort(V.begin(), V.end(), greater<P>()); int ans = 1 << 29; int MAX = 0; for (int i = 0; i < V.size() - 1; i++) { ans = min(ans, MAX + V[i].first); MAX = max(MAX, V[i].second); } cout << ans << endl; }
#include <algorithm> #include <cstdlib> #include <ctime> #include <functional> #include <iostream> #include <limits.h> #include <list> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string.h> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #define P pair<int, int> using namespace std; vector<P> V; signed main() { int a, b, n; cin >> a >> b >> n; for (int i = 0; i < n; i++) { int c, d; cin >> c >> d; V.push_back({c, d}); } V.push_back({0, 0}); sort(V.begin(), V.end(), greater<P>()); int ans = 1 << 29; int MAX = 0; for (int i = 0; i < V.size(); i++) { ans = min(ans, MAX + V[i].first); MAX = max(MAX, V[i].second); } cout << ans << endl; }
[["-", 8, 9, 0, 7, 15, 16, 12, 16, 17, 33], ["-", 8, 9, 0, 7, 15, 16, 12, 16, 12, 13]]
1
209
#include <bits/stdc++.h> using namespace std; const int inf = 1001001001; int main() { int w, h, n; cin >> w >> h >> n; vector<int> a(h + 1, 0); for (int i = 0; i < n; ++i) { int x, y; cin >> x >> y; a[y] = max(a[y], x); } int ans = inf; for (int i = h; i >= 0; --i) { ans = min(ans, i + a[i + 1]); a[i] = max(a[i + 1], a[i]); } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; const int inf = 1001001001; int main() { int w, h, n; cin >> w >> h >> n; vector<int> a(h + 2, 0); for (int i = 0; i < n; ++i) { int x, y; cin >> x >> y; a[y] = max(a[y], x); } int ans = inf; for (int i = h; i >= 0; --i) { ans = min(ans, i + a[i + 1]); a[i] = max(a[i], a[i + 1]); } cout << ans << endl; }
[["-", 0, 43, 49, 50, 51, 4, 0, 16, 12, 13], ["+", 0, 43, 49, 50, 51, 4, 0, 16, 12, 13], ["-", 3, 4, 0, 69, 341, 342, 0, 16, 17, 72], ["-", 3, 4, 0, 69, 341, 342, 0, 16, 12, 13], ["+", 3, 4, 0, 69, 341, 342, 0, 16, 17, 72], ["+", 3, 4, 0, 69, 341, 342, 0, 16, 12, 13]]
1
151
#include <algorithm> #include <cmath> #include <iostream> #define rep(i, a, n) for (int i = a; i < n; i++) using namespace std; int w, h, n; int seg[131072 * 2 - 1]; int N; int p(int i) { return (int)((i - 1) / 2); } void update(int i) { if (i == 0) return; if (seg[p(i)] >= seg[i]) return; seg[p(i)] = seg[i]; update(p(i)); } void init() { rep(i, N - 1, h + N - 1) update(i); } int qmax(int a, int b, int k = 0, int l = 0, int r = N) { if (r <= a || b <= l) return -1; if (a <= l && r <= b) return seg[k]; int vl = qmax(a, b, 2 * k + 1, l, (l + r) / 2); int vr = qmax(a, b, 2 * k + 2, (l + r) / 2, r); return max(vl, vr); } int solve() { int c = 999999999; rep(i, 0, h - 1) { c = min(c, i + qmax(i + 1, h)); } c = min(c, h); return c; } int main() { cin >> w >> h >> n; N = (int)pow(2, (int)ceil(log2(h))); rep(i, 0, n) { int x, y; cin >> x >> y; seg[y + N - 1] = max(seg[y + N - 1], x); } init(); cout << solve() << endl; return 0; }
#include <algorithm> #include <cmath> #include <iostream> #define rep(i, a, n) for (int i = a; i < n; i++) using namespace std; int w, h, n; int seg[131072 * 2 - 1]; int N; int p(int i) { return (int)((i - 1) / 2); } void update(int i) { if (i == 0) return; if (seg[p(i)] >= seg[i]) return; seg[p(i)] = seg[i]; update(p(i)); } void init() { rep(i, N - 1, h + N - 1) update(i); } int qmax(int a, int b, int k = 0, int l = 0, int r = N) { if (r <= a || b <= l) return -1; if (a <= l && r <= b) return seg[k]; int vl = qmax(a, b, 2 * k + 1, l, (l + r) / 2); int vr = qmax(a, b, 2 * k + 2, (l + r) / 2, r); return max(vl, vr); } int solve() { int c = 999999999; rep(i, 0, h - 1) { c = min(c, i + qmax(i + 1, h)); } c = min(c, h - 1); return c; } int main() { cin >> w >> h >> n; N = (int)pow(2, (int)ceil(log2(h))); rep(i, 0, n) { int x, y; cin >> x >> y; seg[y + N - 1] = max(seg[y + N - 1], x); } init(); cout << solve() << endl; return 0; }
[["+", 0, 11, 12, 2, 3, 4, 0, 16, 17, 33], ["+", 0, 11, 12, 2, 3, 4, 0, 16, 12, 13]]
1
406
#include <algorithm> #include <cmath> #include <iostream> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define ll long long #define FOR(i, a, b) for (int i = a; i < b; i++) #define ROF(i, a, b) for (int i = b - 1; i >= a; i--) using namespace std; // const int inf = 1e9 + 7; const int mod = 1e9 + 7; // struct poi { int X; int Y; int Z; bool operator<(const poi &R) const { return X == R.X ? Y == R.Y ? Z < R.Z : Y < R.Y : X < R.X; } }; // ll GCD(ll a, ll b) { return (b == 0) ? (a) : (GCD(b, a % b)); } ll LCM(ll a, ll b) { return a / GCD(a, b) * b; } // int N; struct segtree { // vector<pair<int, int>> dat; // segtree(int n_) { init(n_); } // void init(int n_) { N = 1; while (N < n_) { N *= 2; } dat = vector<pair<int, int>>(N * 2 - 1, make_pair(2147483647, -1)); } // void update(int k, int a) { k += N - 1; dat[k] = make_pair(a, k); while (k) { k = (k - 1) / 2; if (dat[k * 2 + 1].first <= dat[k * 2 + 2].first) { dat[k] = dat[k * 2 + 1]; } else { dat[k] = dat[k * 2 + 2]; } } } //[a,b) pair<int, int> query(int a, int b, int k = 0, int l = 0, int r = -1) { if (r == -1) { r = N; } if (r <= a || b <= l) { return make_pair(2147483647, -1); } else if (a <= l && r <= b) { return dat[k]; } else { pair<int, int> va = query(a, b, k * 2 + 1, l, (l + r) / 2); pair<int, int> vb = query(a, b, k * 2 + 2, (l + r) / 2, r); if (va.first <= vb.first) { return va; } else { return vb; } } } // }; // struct BIT { // int n; vector<int> dat; // BIT(int n_) { init(n_); } // void init(int n_) { n = n_; dat.resize(n + 1, 0); } // void add(int k, int a) { k++; while (k <= n) { dat[k] += a; k += k & -k; } } //[0,k] int query(int k) { k++; int res = 0; while (k) { res += dat[k]; k -= k & -k; } return res; } // }; // int main() { string S; int K; cin >> S >> K; K++; int L = S.size(); segtree seg(L); BIT bit(L); FOR(i, 0, L) { seg.update(i, S[i] - 'a'); bit.add(i, 1); } queue<char> ans; while (K) { int l = 0, r = L - 1; while (r - l > 1) { int m = (l + r) / 2; if (bit.query(m) < K) { l = m; } else { r = m; } } pair<int, int> h = seg.query(0, (L <= r ? L : r + 1)); ans.push('a' + h.first); seg.update(h.second - (N - 1), 2147483647); bit.add(h.second - (N - 1), -1); K -= bit.query(h.second - (N - 1)); if (bit.query(L - 1) == 0) { break; } } vector<char> ret; while (!ans.empty()) { ret.push_back(ans.front()); ans.pop(); } int mm = ret.size(); FOR(i, 0, L) { if (i == 0 && bit.query(0) == 1) { ret.push_back(S[i]); } else if (i != 0 && bit.query(i) - bit.query(i - 1) == 1) { ret.push_back(S[i]); } } while (K < 0) { swap(ret[mm - 1], ret[mm]); mm++; K++; } FOR(i, 0, ret.size()) { cout << ret[i]; } cout << endl; return 0; }
#include <algorithm> #include <cmath> #include <iostream> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define ll long long #define FOR(i, a, b) for (int i = a; i < b; i++) #define ROF(i, a, b) for (int i = b - 1; i >= a; i--) using namespace std; // const int inf = 1e9 + 7; const int mod = 1e9 + 7; // struct poi { int X; int Y; int Z; bool operator<(const poi &R) const { return X == R.X ? Y == R.Y ? Z < R.Z : Y < R.Y : X < R.X; } }; // ll GCD(ll a, ll b) { return (b == 0) ? (a) : (GCD(b, a % b)); } ll LCM(ll a, ll b) { return a / GCD(a, b) * b; } // int N; struct segtree { // vector<pair<int, int>> dat; // segtree(int n_) { init(n_); } // void init(int n_) { N = 1; while (N < n_) { N *= 2; } dat = vector<pair<int, int>>(N * 2 - 1, make_pair(2147483647, -1)); } // void update(int k, int a) { k += N - 1; dat[k] = make_pair(a, k); while (k) { k = (k - 1) / 2; if (dat[k * 2 + 1].first <= dat[k * 2 + 2].first) { dat[k] = dat[k * 2 + 1]; } else { dat[k] = dat[k * 2 + 2]; } } } //[a,b) pair<int, int> query(int a, int b, int k = 0, int l = 0, int r = -1) { if (r == -1) { r = N; } if (r <= a || b <= l) { return make_pair(2147483647, -1); } else if (a <= l && r <= b) { return dat[k]; } else { pair<int, int> va = query(a, b, k * 2 + 1, l, (l + r) / 2); pair<int, int> vb = query(a, b, k * 2 + 2, (l + r) / 2, r); if (va.first <= vb.first) { return va; } else { return vb; } } } // }; // struct BIT { // int n; vector<int> dat; // BIT(int n_) { init(n_); } // void init(int n_) { n = n_; dat.resize(n + 1, 0); } // void add(int k, int a) { k++; while (k <= n) { dat[k] += a; k += k & -k; } } //[0,k] int query(int k) { k++; int res = 0; while (k) { res += dat[k]; k -= k & -k; } return res; } // }; // int main() { string S; int K; cin >> S >> K; K++; int L = S.size(); segtree seg(L); BIT bit(L); FOR(i, 0, L) { seg.update(i, S[i] - 'a'); bit.add(i, 1); } queue<char> ans; while (K) { int l = 0, r = L - 1; while (r - l > 1) { int m = (l + r) / 2; if (bit.query(m) < K) { l = m; } else { r = m; } } pair<int, int> h = seg.query(0, (L <= r ? L : r + 1)); ans.push('a' + h.first); seg.update(h.second - (N - 1), 2147483647); bit.add(h.second - (N - 1), -1); K -= bit.query(h.second - (N - 1)); if (bit.query(L - 1) == 0) { break; } } vector<char> ret; while (!ans.empty()) { ret.push_back(ans.front()); ans.pop(); } int mm = ret.size(); FOR(i, 0, L) { if (i == 0 && bit.query(0) == 1) { ret.push_back(S[i]); } else if (i != 0 && bit.query(i) - bit.query(i - 1) == 1) { ret.push_back(S[i]); } } while (K <= 0) { swap(ret[mm - 1], ret[mm]); mm++; K++; } FOR(i, 0, ret.size()) { cout << ret[i]; } cout << endl; return 0; }
[["-", 8, 9, 0, 52, 15, 339, 51, 16, 17, 18], ["+", 8, 9, 0, 52, 15, 339, 51, 16, 17, 19]]
1
1,095
#include <algorithm> #include <cfloat> #include <cmath> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <string> #include <vector> typedef long long int ll; typedef unsigned long long int ull; #define BIG_NUM 2000000000 #define MOD 1000000007 #define EPS 0.000000001 using namespace std; #define NUM 2147483647 struct Info { int value, index; }; int N, length; Info *table; int BIT[200001]; void add(int loc, int value) { BIT[loc] += value; loc += loc & -loc; while (loc <= length) { BIT[loc] += value; loc += loc & -loc; } } int getSum(int loc) { int sum = BIT[loc]; loc -= loc & -loc; while (loc > 0) { sum += BIT[loc]; loc -= loc & -loc; } return sum; } int calc(int left, int right) { return getSum(right) - getSum(left - 1); } void init(int first_N) { while (N < first_N) N *= 2; } void update(int loc, int value) { loc += N - 1; table[loc].value = value; table[loc].index = loc - (N - 1); if (N == 1) return; int parent = (loc - 1) / 2; while (true) { if (table[2 * parent + 2].value <= table[2 * parent + 1].value) { table[parent].value = table[2 * parent + 2].value; table[parent].index = table[2 * parent + 2].index; } else { table[parent].value = table[2 * parent + 1].value; table[parent].index = table[2 * parent + 1].index; } if (parent == 0) break; else { parent = (parent - 1) / 2; } } } Info query(int search_left, int search_right, int node_id, int node_left, int node_right) { Info ret; if (search_right < node_left || search_left > node_right) { ret.value = NUM; return ret; } if (search_left <= node_left && search_right >= node_right) { return table[node_id]; } Info left_min = query(search_left, search_right, 2 * node_id + 1, node_left, (node_left + node_right) / 2); Info right_min = query(search_left, search_right, 2 * node_id + 2, (node_left + node_right) / 2 + 1, node_right); if (right_min.value <= left_min.value) { return right_min; } else { return left_min; } } int main() { char *buf = new char[200001]; scanf("%s", buf); for (length = 0; buf[length] != '\0'; length++) ; int first_N; table = (Info *)malloc(sizeof(Info) * 550000); N = 1; first_N = length; init(first_N); for (int i = 0; i <= length; i++) BIT[i] = 0; for (int i = 1; i <= length; i++) add(i, 1); for (int i = 0; i <= 2 * N - 2; i++) table[i].value = NUM; for (int i = 0; i < length; i++) update(i, buf[i] - 'a'); int K; scanf("%d", &K); if (K == 0) { printf("%s\n", buf); return 0; } Info ret_query; queue<char> Q; int left, right, m; int calc_right, tmp; for (int i = 0; i <= length - 2;) { if (calc(i + 1, i + 1) == 0) { i++; continue; } left = i + 1, right = length - 1, m = (left + right) / 2; calc_right = BIG_NUM; while (left <= right) { tmp = calc((i + 1) + 1, m + 1); if (tmp == K) { calc_right = m; break; } else if (tmp < K) { calc_right = m; left = m + 1; } else { right = m - 1; } m = (left + right) / 2; } if (calc_right == BIG_NUM) break; ret_query = query(i + 1, calc_right, 0, 0, N - 1); if (buf[i] - 'a' > ret_query.value) { Q.push(buf[ret_query.index]); update(ret_query.index, NUM); add(ret_query.index + 1, -1); K -= (calc(i + 1, ret_query.index)); if (K <= 0) break; } else { Q.push(buf[i]); update(i, NUM); add(i + 1, -1); i++; } } while (!Q.empty()) { printf("%c", Q.front()); Q.pop(); } for (int i = 1; i <= length; i++) { if (calc(i, i) == 1) { printf("%c", buf[i - 1]); } } printf("\n"); return 0; }
#include <algorithm> #include <cfloat> #include <cmath> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <string> #include <vector> typedef long long int ll; typedef unsigned long long int ull; #define BIG_NUM 2000000000 #define MOD 1000000007 #define EPS 0.000000001 using namespace std; #define NUM 2147483647 struct Info { int value, index; }; int N, length; Info *table; int BIT[200001]; void add(int loc, int value) { BIT[loc] += value; loc += loc & -loc; while (loc <= length) { BIT[loc] += value; loc += loc & -loc; } } int getSum(int loc) { int sum = BIT[loc]; loc -= loc & -loc; while (loc > 0) { sum += BIT[loc]; loc -= loc & -loc; } return sum; } int calc(int left, int right) { return getSum(right) - getSum(left - 1); } void init(int first_N) { while (N < first_N) N *= 2; } void update(int loc, int value) { loc += N - 1; table[loc].value = value; table[loc].index = loc - (N - 1); if (N == 1) return; int parent = (loc - 1) / 2; while (true) { if (table[2 * parent + 2].value < table[2 * parent + 1].value) { table[parent].value = table[2 * parent + 2].value; table[parent].index = table[2 * parent + 2].index; } else { table[parent].value = table[2 * parent + 1].value; table[parent].index = table[2 * parent + 1].index; } if (parent == 0) break; else { parent = (parent - 1) / 2; } } } Info query(int search_left, int search_right, int node_id, int node_left, int node_right) { Info ret; if (search_right < node_left || search_left > node_right) { ret.value = NUM; return ret; } if (search_left <= node_left && search_right >= node_right) { return table[node_id]; } Info left_min = query(search_left, search_right, 2 * node_id + 1, node_left, (node_left + node_right) / 2); Info right_min = query(search_left, search_right, 2 * node_id + 2, (node_left + node_right) / 2 + 1, node_right); if (left_min.value <= right_min.value) { return left_min; } else { return right_min; } } int main() { char *buf = new char[200001]; scanf("%s", buf); for (length = 0; buf[length] != '\0'; length++) ; int first_N; table = (Info *)malloc(sizeof(Info) * 550000); N = 1; first_N = length; init(first_N); for (int i = 0; i <= length; i++) BIT[i] = 0; for (int i = 1; i <= length; i++) add(i, 1); for (int i = 0; i <= 2 * N - 2; i++) table[i].value = NUM; for (int i = 0; i < length; i++) update(i, buf[i] - 'a'); int K; scanf("%d", &K); if (K == 0) { printf("%s\n", buf); return 0; } Info ret_query; queue<char> Q; int left, right, m; int calc_right, tmp; for (int i = 0; i <= length - 2;) { if (calc(i + 1, i + 1) == 0) { i++; continue; } left = i + 1, right = length - 1, m = (left + right) / 2; calc_right = BIG_NUM; while (left <= right) { tmp = calc((i + 1) + 1, m + 1); if (tmp == K) { calc_right = m; break; } else if (tmp < K) { calc_right = m; left = m + 1; //???????????? } else { right = m - 1; } m = (left + right) / 2; } if (calc_right == BIG_NUM) break; ret_query = query(i + 1, calc_right, 0, 0, N - 1); if (buf[i] - 'a' > ret_query.value) { Q.push(buf[ret_query.index]); update(ret_query.index, NUM); add(ret_query.index + 1, -1); K -= (calc(i + 1, ret_query.index)); if (K <= 0) break; } else { Q.push(buf[i]); update(i, NUM); add(i + 1, -1); i++; } } while (!Q.empty()) { printf("%c", Q.front()); Q.pop(); } for (int i = 1; i <= length; i++) { if (calc(i, i) == 1) { printf("%c", buf[i - 1]); } } printf("\n"); return 0; }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 19], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 18], ["-", 0, 57, 15, 339, 51, 16, 31, 118, 28, 22], ["+", 0, 57, 15, 339, 51, 16, 31, 118, 28, 22], ["-", 0, 57, 15, 339, 51, 16, 12, 118, 28, 22], ["+", 0, 57, 15, 339, 51, 16, 12, 118, 28, 22], ["-", 8, 9, 0, 57, 64, 9, 0, 37, 0, 22], ["+", 8, 9, 0, 57, 64, 9, 0, 37, 0, 22], ["-", 0, 57, 75, 76, 0, 9, 0, 37, 0, 22], ["+", 0, 57, 75, 76, 0, 9, 0, 37, 0, 22]]
1
1,121
#include <stdio.h> int main(void) { int n, i, j, count, tmp; char s[10000] = {0}, t[10000] = {0}; while (scanf("%d", &n), n) { count = 0; scanf("%s", s); while (n--) { count = 0; for (i = 0; s[i] != '\0'; i++) { for (j = 1; s[i] == s[i + j]; j++) ; tmp = j - 1; j--; if (j > 99) { t[count++] = j / 100 + '0'; j %= 100; t[count++] = j / 10 + '0'; j %= 10; t[count++] = j + '0'; t[count++] = s[i]; } else if (j > 9) { t[count++] = j / 10 + '0'; j %= 10; t[count++] = j + '0'; t[count++] = s[i]; } else { t[count++] = j + '0'; t[count++] = s[i]; } i = i + tmp - 1; } for (i = 0; i < 10000; i++) { s[i] = t[i]; t[i] = 0; } } printf("%s\n", s); } return 0; }
#include <stdio.h> int main(void) { int n, i, j, count, tmp; char s[1000000] = {0}, t[1000000] = {0}; while (scanf("%d", &n), n) { count = 0; scanf("%s", s); while (n--) { count = 0; for (i = 0; s[i] != '\0'; i++) { for (j = 1; s[i] == s[i + j - 1]; j++) ; tmp = j - 1; j--; if (j > 99) { t[count++] = j / 100 + '0'; j %= 100; t[count++] = j / 10 + '0'; j %= 10; t[count++] = j + '0'; t[count++] = s[i]; } else if (j > 9) { t[count++] = j / 10 + '0'; j %= 10; t[count++] = j + '0'; t[count++] = s[i]; } else { t[count++] = j + '0'; t[count++] = s[i]; } i = i + tmp - 1; } for (i = 0; i < 1000000; i++) { s[i] = t[i]; t[i] = 0; } } printf("%s\n", s); } return 0; }
[["-", 8, 9, 0, 43, 49, 50, 49, 80, 81, 13], ["+", 8, 9, 0, 43, 49, 50, 49, 80, 81, 13], ["+", 0, 7, 15, 16, 12, 69, 71, 16, 17, 33], ["+", 0, 7, 15, 16, 12, 69, 71, 16, 12, 13], ["-", 0, 52, 8, 9, 0, 7, 15, 16, 12, 13], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 12, 13]]
0
326
#include <stdio.h> #include <string.h> int main(void) { char str1[10000001], str2[10000001], fig, tmp; int i, n, idx1, idx2, sum; while (1) { scanf("%d", &n); if (!n) break; scanf("%s", str1); for (i = 0; i < n; i++) { idx1 = idx2 = 0; while (1) { if (sscanf(str1 + idx1, "%c", &fig) == EOF) break; sum = 1; while (sscanf(str1 + ++idx1, "%c", &tmp) != EOF && fig == tmp) sum++; sprintf(str2 + idx2++, "%d%c", sum, fig); do { idx2++; } while (sum /= 10); } strcpy(str1, str2); } printf("%s\n", str1); } return 0; }
#include <stdio.h> #include <string.h> int main(void) { char str1[100000], str2[100000], fig, tmp; int i, n, idx1, idx2, sum; while (1) { scanf("%d", &n); if (!n) break; scanf("%s", str1); for (i = 0; i < n; i++) { idx1 = idx2 = 0; while (1) { if (sscanf(str1 + idx1, "%c", &fig) == EOF) break; sum = 1; while (sscanf(str1 + ++idx1, "%c", &tmp) != EOF && fig == tmp) sum++; sprintf(str2 + idx2++, "%d%c", sum, fig); do { idx2++; } while (sum /= 10); } strcpy(str1, str2); } printf("%s\n", str1); } return 0; }
[["-", 0, 14, 8, 9, 0, 43, 49, 80, 81, 13], ["+", 0, 14, 8, 9, 0, 43, 49, 80, 81, 13]]
0
195
#include <iostream> #include <stdio.h> #include <string.h> using namespace std; int n; char s[21][1000000]; int main() { int i, j, len; char bef, now; int cnt, cor; while (1) { cin >> n >> s[0]; if (!n) break; for (i = 0; i < n; i++) { len = strlen(s[i]); cnt = 1; cor = 0; bef = s[i][0]; for (j = 1; j < len; j++) { now = s[i][j]; if (bef == now) { cnt++; } else { cor += sprintf(s[i + 1] + cor, "%d%c", cnt, bef); cnt = 1; //すでに新しい文字を1文字読み込んでいる } bef = now; } cor += sprintf(s[i + 1] + cor, "%d%c", cnt, bef); // s[i+1]を何回でも使えるようにする工夫 s[i + 1][cor] = '\0'; } cout << s[n]; } return 0; }
#include <iostream> #include <stdio.h> #include <string.h> using namespace std; int n; char s[21][1000000]; int main() { int i, j, len; char bef, now; int cnt, cor; while (1) { cin >> n >> s[0]; if (!n) break; for (i = 0; i < n; i++) { len = strlen(s[i]); cnt = 1; cor = 0; bef = s[i][0]; for (j = 1; j < len; j++) { now = s[i][j]; if (bef == now) { cnt++; } else { cor += sprintf(s[i + 1] + cor, "%d%c", cnt, bef); cnt = 1; //すでに新しい文字を1文字読み込んでいる } bef = now; } cor += sprintf(s[i + 1] + cor, "%d%c", cnt, bef); // s[i+1]を何回でも使えるようにする工夫 s[i + 1][cor] = '\0'; } cout << s[n] << endl; } return 0; }
[["+", 0, 52, 8, 9, 0, 1, 0, 16, 17, 151], ["+", 0, 52, 8, 9, 0, 1, 0, 16, 12, 22]]
1
227
#include <algorithm> #include <cstdio> #include <cstdlib> #include <iostream> #include <math.h> #include <queue> #include <sstream> #include <string> #include <vector> using namespace std; #define FOR(I, N) for (int I = 0; I < (int)(N); I++) #define FIN(V) cout << V << endl #define pb push_back #define INF (1 << 30) typedef pair<int, int> P; string itos(int i) { stringstream ss; ss << i; return ss.str(); } int stoi(string str) { stringstream ss; ss << str; int val; ss >> val; // 10 return val; } int main(void) { int N; while (cin >> N) { if (N == 0) break; string S, L; cin >> S; for (int i = 0; i < N; i++) { int cnt = 1; for (int j = 0; j < S.size(); j++) { if (S[j] == S[j + 1]) { cnt++; } else { L.append(itos(cnt)); L.push_back(S[j]); cnt = 1; } } cnt = 1; S = L; L.clear(); FIN(S); } FIN(S); } return 0; }
#include <algorithm> #include <cstdio> #include <cstdlib> #include <iostream> #include <math.h> #include <queue> #include <sstream> #include <string> #include <vector> using namespace std; #define FOR(I, N) for (int I = 0; I < (int)(N); I++) #define FIN(V) cout << V << endl #define pb push_back #define INF (1 << 30) typedef pair<int, int> P; string itos(int i) { stringstream ss; ss << i; return ss.str(); } int stoi(string str) { stringstream ss; ss << str; int val; ss >> val; // 10 return val; } int main(void) { int N; while (cin >> N) { if (N == 0) break; string S, L; cin >> S; for (int i = 0; i < N; i++) { int cnt = 1; for (int j = 0; j < S.size(); j++) { if (S[j] == S[j + 1]) { cnt++; } else { L.append(itos(cnt)); L.push_back(S[j]); cnt = 1; } } cnt = 1; S = L; L.clear(); } FIN(S); } return 0; }
[["-", 0, 7, 8, 9, 0, 1, 0, 2, 63, 22], ["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 24], ["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 22], ["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 25], ["-", 8, 9, 0, 7, 8, 9, 0, 1, 0, 35]]
1
248
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <map> #include <set> #include <sstream> #include <string> #include <vector> #define rep(i, l, n) for (lint i = l; i < n; i++) #define rer(i, l, n) for (lint i = l; i <= n; i++) #define all(a) a.begin(), a.end() #define o(a) cout << a << endl #define pb(a) push_back(a) #define mk(a, b) make_pair(a, b) #define fi first #define se second using namespace std; typedef long long lint; typedef vector<int> vi; typedef vector<lint> vli; typedef vector<vi> vvi; typedef pair<int, int> pii; string change(int n) { stringstream ss; ss << n; return ss.str(); } int main() { int n; string s; while (1) { cin >> n; if (n == 0) break; cin >> s; string t = ""; rep(i, 0, n) { int bef = s[0] - '0', suc = 1; rep(j, 1, s.size()) { int now = s[j] - '0'; if (now == bef) { suc++; } else { t += change(suc); t += '0' + bef; bef = now; suc = 1; } if (j == s.size() - 1) { t += change(suc); t += '0' + bef; } } if (s.size() == 1) { t += '1'; t += s[0]; } o(t); s = t; t.clear(); } o(t); } }
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <map> #include <set> #include <sstream> #include <string> #include <vector> #define rep(i, l, n) for (lint i = l; i < n; i++) #define rer(i, l, n) for (lint i = l; i <= n; i++) #define all(a) a.begin(), a.end() #define o(a) cout << a << endl #define pb(a) push_back(a) #define mk(a, b) make_pair(a, b) #define fi first #define se second using namespace std; typedef long long lint; typedef vector<int> vi; typedef vector<lint> vli; typedef vector<vi> vvi; typedef pair<int, int> pii; string change(int n) { stringstream ss; ss << n; return ss.str(); } int main() { int n; string s; while (1) { cin >> n; if (n == 0) break; cin >> s; string t = ""; rep(i, 0, n) { int bef = s[0] - '0', suc = 1; rep(j, 1, s.size()) { int now = s[j] - '0'; if (now == bef) { suc++; } else { t += change(suc); t += '0' + bef; bef = now; suc = 1; } if (j == s.size() - 1) { t += change(suc); t += '0' + bef; } } if (s.size() == 1) { t += '1'; t += s[0]; } s = t; t.clear(); } o(s); } }
[["-", 8, 9, 0, 9, 0, 1, 0, 2, 63, 22], ["-", 0, 9, 0, 1, 0, 2, 3, 4, 0, 24], ["-", 0, 9, 0, 1, 0, 2, 3, 4, 0, 22], ["-", 0, 9, 0, 1, 0, 2, 3, 4, 0, 25], ["-", 0, 52, 8, 9, 0, 9, 0, 1, 0, 35], ["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 22], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 22]]
1
336
#include <cstring> #include <iostream> #define REP(i, a, n) for (int i = a; i < n; i++) using namespace std; int n; char str[21][10000]; int main(void) { while (cin >> n, n != 0) { cin >> str[0]; REP(i, 0, n) { int j = 0; int k = 0; int len = strlen(str[i]); while (k < len) { int s = k; while (k < len && str[i][k] == str[i][k + 1]) k++; int cnt = k - s + 1; int l = 0; int d[100]; while (cnt != 0) { d[l] = cnt % 10; cnt /= 10; l++; } while (l > 0) { str[i + 1][j++] = '0' + d[--l]; } str[i + 1][j++] = str[i][s]; k++; } str[i][j++] = '\0'; } cout << str[n] << endl; } return 0; }
#include <cstring> #include <iostream> #define REP(i, a, n) for (int i = a; i < n; i++) using namespace std; int n; char str[21][100000]; int main(void) { while (cin >> n, n != 0) { cin >> str[0]; REP(i, 0, n) { int j = 0; int k = 0; int len = strlen(str[i]); while (k < len) { int s = k; while (k < len && str[i][k] == str[i][k + 1]) k++; int cnt = k - s + 1; int l = 0; int d[100]; while (cnt != 0) { d[l] = cnt % 10; cnt /= 10; l++; } while (l > 0) { str[i + 1][j++] = '0' + d[--l]; } str[i + 1][j++] = str[i][s]; k++; } str[i + 1][j++] = '\0'; } cout << str[n] << endl; } return 0; }
[["-", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13], ["+", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13], ["+", 31, 69, 28, 69, 341, 342, 0, 16, 17, 72], ["+", 31, 69, 28, 69, 341, 342, 0, 16, 12, 13]]
1
248
#include <cstdio> #include <cstdlib> #include <iostream> #include <string> using namespace std; int N; string S; string::iterator CountSeqStr(string::iterator start, string *s, int *n) { *s = *start; *n = 0; string tmp; for (; tmp = *start, tmp == *s && start != S.end(); ++start) { (*n)++; } return --start; } string F() { string retS; string s; string::iterator it; int n; for (it = S.begin(); it != S.end(); ++it) { it = CountSeqStr(it, &s, &n); char *strn; sprintf(strn, "%d", n); retS.append(string(strn)); retS.append(s); } return retS; } int main() { while (cin >> N && N) { cin >> S; for (int i = 0; i < N; i++) { string tmp; S = F(); } cout << S << endl; } return 0; }
#include <cstdio> #include <cstdlib> #include <iostream> #include <string> using namespace std; int N; string S; string::iterator CountSeqStr(string::iterator start, string *s, int *n) { *s = *start; *n = 0; string tmp; for (; tmp = *start, tmp == *s && start != S.end(); ++start) { (*n)++; } return --start; } string F() { string retS; string s; string::iterator it; int n; for (it = S.begin(); it != S.end(); ++it) { it = CountSeqStr(it, &s, &n); char strn[10000]; sprintf(strn, "%d", n); retS.append(string(strn)); retS.append(s); } return retS; } int main() { while (cin >> N && N) { cin >> S; for (int i = 0; i < N; i++) { string tmp; S = F(); } cout << S << endl; } return 0; }
[["-", 0, 7, 8, 9, 0, 43, 49, 84, 0, 48], ["+", 0, 7, 8, 9, 0, 43, 49, 80, 0, 70], ["+", 0, 7, 8, 9, 0, 43, 49, 80, 81, 13], ["+", 0, 7, 8, 9, 0, 43, 49, 80, 0, 73]]
1
233
while True: n=eval(input()) if n==0: break ar=input()+' ' for i in range(n): print(i) cnt=1 tmp='' for i in range(len(ar)-1): if ar[i]==ar[i+1]: cnt+=1 else: tmp+="%d%s"%(cnt,ar[i]) cnt=1 ar=tmp+' ' print(ar.rstrip())
while True: n=eval(input()) if n==0: break ar=input()+' ' for i in range(n): cnt=1 tmp='' for i in range(len(ar)-1): if ar[i]==ar[i+1]: cnt+=1 else: tmp+="%d%s"%(cnt,ar[i]) cnt=1 ar=tmp+' ' print(ar.rstrip())
[["-", 0, 7, 8, 196, 0, 1, 0, 652, 63, 22], ["-", 8, 196, 0, 1, 0, 652, 3, 4, 0, 24], ["-", 8, 196, 0, 1, 0, 652, 3, 4, 0, 22], ["-", 8, 196, 0, 1, 0, 652, 3, 4, 0, 25]]
5
108
#include <iostream> #include <map> #include <vector> using namespace std; int main() { int n; vector<char> str; while (cin >> n, n) { map<char, char> m; char a, b; for (int i = 0; i < n; i++) { cin >> a >> b; m[a] = b; } cin >> n; for (int i = 0; i < n; i++) { cin >> a; if (m[a]) { str.push_back(m[a]); } else { str.push_back(a); } } vector<char>::iterator it = str.begin(); while (it != str.end()) { cout << *it; it++; } cout << endl; } return 0; }
#include <iostream> #include <map> #include <vector> using namespace std; int main() { int n; vector<char> str; while (cin >> n, n) { map<char, char> m; char a, b; for (int i = 0; i < n; i++) { cin >> a >> b; m[a] = b; } cin >> n; for (int i = 0; i < n; i++) { cin >> a; if (m[a]) { str.push_back(m[a]); } else { str.push_back(a); } } vector<char>::iterator it = str.begin(); while (it != str.end()) { cout << *it; it++; } cout << endl; str.clear(); } return 0; }
[["+", 8, 9, 0, 1, 0, 2, 63, 118, 28, 22], ["+", 8, 9, 0, 1, 0, 2, 63, 118, 17, 131], ["+", 8, 9, 0, 1, 0, 2, 63, 118, 119, 120], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 24], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 25], ["+", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35]]
1
171
#include <algorithm> #include <cmath> #include <cstdlib> #include <iostream> #include <string> #include <vector> using namespace std; int main() { string s1, s2, tmp, ans; int n, m; while (1) { s1.clear(); s2.clear(); ans.clear(); cin >> n; if (n == 0) return 0; for (int i = 0; i < n; i++) { cin >> tmp; s1 += tmp; cin >> tmp; s2 += tmp; } cin >> m; for (int i = 0; i < m; i++) { cin >> tmp; ans += tmp; for (int j = 0; j < n; j++) { if (ans[i] == s1[j]) ans[i] = s2[j]; } } cout << ans << endl; } }
#include <algorithm> #include <cmath> #include <cstdlib> #include <iostream> #include <string> #include <vector> using namespace std; int main() { string s1, s2, tmp, ans; int n, m; while (1) { s1.clear(); s2.clear(); ans.clear(); cin >> n; if (n == 0) return 0; for (int i = 0; i < n; i++) { cin >> tmp; s1 += tmp; cin >> tmp; s2 += tmp; } cin >> m; for (int i = 0; i < m; i++) { cin >> tmp; ans += tmp; for (int j = 0; j < n; j++) { if (ans[i] == s1[j]) { ans[i] = s2[j]; break; } } } cout << ans << endl; } }
[["+", 0, 7, 8, 9, 0, 57, 64, 9, 0, 45], ["+", 8, 9, 0, 57, 64, 9, 0, 1, 0, 35], ["+", 8, 9, 0, 57, 64, 9, 0, 93, 0, 94], ["+", 0, 7, 8, 9, 0, 57, 64, 9, 0, 46]]
1
177
#include <iostream> #include <vector> using namespace std; int main(void) { while (1) { int n; cin >> n; if (!n) break; vector<char> v1, v2; for (int i = 0; i < n; i++) { char c; cin >> c; v1.push_back(c); cin >> c; v2.push_back(c); } int m; cin >> m; for (int i = 0; i < m; i++) { char b; cin >> b; for (int j = 0; j < n; j++) if (b == v1[j]) b = v2[j]; cout << b; } cout << endl; } }
#include <iostream> #include <vector> using namespace std; int main(void) { while (1) { int n; cin >> n; if (!n) break; vector<char> v1, v2; for (int i = 0; i < n; i++) { char c; cin >> c; v1.push_back(c); cin >> c; v2.push_back(c); } int m; cin >> m; for (int i = 0; i < m; i++) { char b; cin >> b; for (int j = 0; j < n; j++) { if (b == v1[j]) { b = v2[j]; break; } } cout << b; } cout << endl; } }
[["+", 0, 7, 8, 9, 0, 7, 8, 9, 0, 45], ["+", 0, 7, 8, 9, 0, 57, 64, 9, 0, 45], ["+", 8, 9, 0, 57, 64, 9, 0, 1, 0, 35], ["+", 8, 9, 0, 57, 64, 9, 0, 93, 0, 94], ["+", 0, 7, 8, 9, 0, 57, 64, 9, 0, 46], ["+", 0, 7, 8, 9, 0, 7, 8, 9, 0, 46]]
1
152
#include <iostream> #include <map> using namespace std; int main(void) { map<char, char> chgMap; int num; char in1, in2; int i; while (true) { cin >> num; if (num == 0) { break; } for (i = 0; i < num; i++) { cin >> in1 >> in2; chgMap[in1] = in2; } cin >> num; for (i = 0; i < num; i++) { cin >> in1; if (chgMap.find(in1) != chgMap.end()) { cout << chgMap[in1]; } else { cout << in1; } } cout << endl; } return 0; }
#include <iostream> #include <map> using namespace std; int main(void) { map<char, char> chgMap; int num; char in1, in2; int i; while (true) { cin >> num; if (num == 0) { break; } chgMap.clear(); for (i = 0; i < num; i++) { cin >> in1 >> in2; chgMap[in1] = in2; } cin >> num; for (i = 0; i < num; i++) { cin >> in1; if (chgMap.find(in1) != chgMap.end()) { cout << chgMap[in1]; } else { cout << in1; } } cout << endl; } return 0; }
[["+", 8, 9, 0, 1, 0, 2, 63, 118, 28, 22], ["+", 8, 9, 0, 1, 0, 2, 63, 118, 17, 131], ["+", 8, 9, 0, 1, 0, 2, 63, 118, 119, 120], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 24], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 25], ["+", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35]]
1
143
#include <algorithm> #include <cstdio> #include <list> using namespace std; int n, m; int main() { while (true) { list<char> map_1; list<char> map_2; list<char>::iterator map; list<char>::iterator map2; scanf("%d", &n); if (n == 0) break; for (int i = 0; i < n; i++) { char a, b; scanf(" %c %c", &a, &b); map_1.push_back(a); map_2.push_back(b); } scanf("%d", &m); for (int i = 0; i < m; i++) { char c; scanf(" %c", &c); if (find(map_1.begin(), map_1.end(), c) == map_1.end()) printf("%c", c); else { map = map_1.begin(); map2 = map_2.begin(); while (true) { if (*map == c) break; map++; map2++; } printf("%c", *map2); } } } return 0; }
#include <algorithm> #include <cstdio> #include <list> using namespace std; int n, m; int main() { while (true) { list<char> map_1; list<char> map_2; list<char>::iterator map; list<char>::iterator map2; scanf("%d", &n); if (n == 0) break; for (int i = 0; i < n; i++) { char a, b; scanf(" %c %c", &a, &b); map_1.push_back(a); map_2.push_back(b); } scanf("%d", &m); for (int i = 0; i < m; i++) { char c; scanf(" %c", &c); if (find(map_1.begin(), map_1.end(), c) == map_1.end()) printf("%c", c); else { map = map_1.begin(); map2 = map_2.begin(); while (true) { if (*map == c) break; map++; map2++; } printf("%c", *map2); } } printf("\n"); } return 0; }
[["+", 0, 52, 8, 9, 0, 1, 0, 2, 63, 22], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 24], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 62], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 44], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 25], ["+", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35]]
1
247
#include <stdio.h> int main() { int n, m; int i, j; char c[10000000][2]; char t[10000000]; int f; while (scanf("%d", &n), n) { for (i = 0; i < n; i++) { scanf("%s", &c[i][0]); scanf("%s", &c[i][1]); } scanf("%d", &m); for (i = 0; i < m; i++) { scanf(" %s", &t[i]); } for (i = 0; i < m; i++) { f = -1; for (j = 0; j < n; j++) { if (t[i] == c[j][0]) { f = j; } } if (f != -1) { printf("%c", c[f][1]); } else if (f == -1) { printf("%c", t[i]); } } printf("\n"); } return 0; }
#include <stdio.h> int main() { int n, m; int i, j; char c[1000000][2]; char t[1000000]; int f; while (scanf("%d", &n), n) { for (i = 0; i < n; i++) { scanf("%s", &c[i][0]); scanf("%s", &c[i][1]); } scanf("%d", &m); for (i = 0; i < m; i++) { scanf(" %s", &t[i]); } for (i = 0; i < m; i++) { f = -1; for (j = 0; j < n; j++) { if (t[i] == c[j][0]) { f = j; } } if (f != -1) { printf("%c", c[f][1]); } else if (f == -1) { printf("%c", t[i]); } } printf("\n"); } return 0; }
[["-", 8, 9, 0, 43, 49, 80, 49, 80, 81, 13], ["+", 8, 9, 0, 43, 49, 80, 49, 80, 81, 13], ["-", 0, 14, 8, 9, 0, 43, 49, 80, 81, 13], ["+", 0, 14, 8, 9, 0, 43, 49, 80, 81, 13]]
1
246
#include <iostream> #include <string> using namespace std; int main() { int n; int c; string d; int m; int e; int f; while (cin >> n) { if (n != 0) { string a[n]; string b[n]; c = 0; while (c < n) { cin >> a[c]; cin >> b[c]; c = c + 1; } cin >> m; c = 0; while (c < m) { cin >> d; e = 0; f = 0; while (e < n) { if (d == a[e]) { cout << b[e]; f = 1; } e = e + 1; } if (f == 0) { cout << d; } c = c + 1; } } } }
#include <iostream> #include <string> using namespace std; int main() { int n; int c; string d; int m; int e; int f; while (cin >> n) { if (n != 0) { string a[n]; string b[n]; c = 0; while (c < n) { cin >> a[c]; cin >> b[c]; c = c + 1; } cin >> m; c = 0; while (c < m) { cin >> d; e = 0; f = 0; while (e < n) { if (d == a[e]) { cout << b[e]; f = 1; } e = e + 1; } if (f == 0) { cout << d; } c = c + 1; } cout << endl; } } }
[["+", 0, 57, 64, 9, 0, 1, 0, 16, 31, 22], ["+", 0, 57, 64, 9, 0, 1, 0, 16, 17, 151], ["+", 0, 57, 64, 9, 0, 1, 0, 16, 12, 22], ["+", 8, 9, 0, 57, 64, 9, 0, 1, 0, 35]]
1
174
#include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <string> #include <utility> #include <vector> using namespace std; // conversion //------------------------------------------ inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } // typedef //------------------------------------------ typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<string> VS; typedef pair<int, int> PII; typedef pair<int, PII> TIII; typedef long long LL; typedef vector<LL> VLL; // container util //------------------------------------------ #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define PB push_back #define MP make_pair #define SZ(a) int((a).size()) #define EACH(i, c) \ for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define EXIST(s, e) ((s).find(e) != (s).end()) #define SORT(c) sort((c).begin(), (c).end()) #define MT(a, b, c) MP(a, MP(b, c)) // repetition//------------------------------------------ #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) char tr[128]; char str[111111111]; int main() { while (1) { REP(i, 128) tr[i] = i; int d, n; cin >> d; if (d == 0) break; REP(i, d) { char s[2][2]; scanf("%s", s[0]); scanf("%s", s[1]); tr[s[0][0]] = s[1][0]; } cin >> n; REP(i, n) { char t[2]; scanf("%s", t); str[i] = tr[t[0]]; } str[n] = 0; printf("%s", str); } return 0; }
#include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <string> #include <utility> #include <vector> using namespace std; // conversion //------------------------------------------ inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } // typedef //------------------------------------------ typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<string> VS; typedef pair<int, int> PII; typedef pair<int, PII> TIII; typedef long long LL; typedef vector<LL> VLL; // container util //------------------------------------------ #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define PB push_back #define MP make_pair #define SZ(a) int((a).size()) #define EACH(i, c) \ for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define EXIST(s, e) ((s).find(e) != (s).end()) #define SORT(c) sort((c).begin(), (c).end()) #define MT(a, b, c) MP(a, MP(b, c)) // repetition//------------------------------------------ #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) char tr[128]; char str[111111111]; int main() { while (1) { REP(i, 128) tr[i] = i; int d, n; cin >> d; if (d == 0) break; REP(i, d) { char s[2][2]; scanf("%s", s[0]); scanf("%s", s[1]); tr[s[0][0]] = s[1][0]; } cin >> n; REP(i, n) { char t[2]; scanf("%s", t); str[i] = tr[t[0]]; } str[n] = 0; printf("%s\n", str); } return 0; }
[["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 44]]
1
413
#include <iostream> #include <map> using namespace std; int main() { int n, m; char a, b; map<char, char> ma; while (cin >> n, n) { for (int i = 0; i < n; i++) { cin >> a >> b; ma[a] = b; } cin >> m; for (int i = 0; i < m; i++) { cin >> a; if (ma[a] != '\0') cout << ma[a]; else cout << a; } cout << endl; } }
#include <iostream> #include <map> using namespace std; int main() { int n, m; char a, b; map<char, char> ma; while (cin >> n, n) { ma.clear(); for (int i = 0; i < n; i++) { cin >> a >> b; ma[a] = b; } cin >> m; for (int i = 0; i < m; i++) { cin >> a; if (ma[a] != '\0') cout << ma[a]; else cout << a; } cout << endl; } }
[["+", 8, 9, 0, 1, 0, 2, 63, 118, 28, 22], ["+", 8, 9, 0, 1, 0, 2, 63, 118, 17, 131], ["+", 8, 9, 0, 1, 0, 2, 63, 118, 119, 120], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 24], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 25], ["+", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35]]
1
122