Codeforces STC #2 E "Anfisa the Monkey"
問題:http://codeforces.com/contest/44/problem/E
本番?
行数,行の最小文字数および最大文字数と文が与えられる.最大最小の文字数制限を満たした文字列によって行数分分割したものを求める.
分割をDFSで行った.枝刈りは残りの行数から見て最大最小の制限上答えが出ないものを行った.
最初はBFSでやってテストケース6でMLE,そりゃそうだとDFSに切り替えて枝刈りせずにテストケース18とテストケース6でTLE.枝刈りやってAC.再帰は慣れてないので終了部分がキレイじゃない.そして本当はこんな面倒な方法をする必要もなく,最小文字数と最大文字数の間の一つの文字数で切ったものk-1個とその残りの文字数1個でいいみたい.
import java.util.*; public class E_AnfisaTheMonkey { public static void main(String[] args) { Scanner s = new Scanner(System.in); k = s.nextInt(); a = s.nextInt(); b = s.nextInt(); str = s.next(); list = new ArrayDeque<Integer>(); f(0); System.out.println("No solution"); } static String str; static Deque<Integer> list; static int a, b, k; static void f(int sum) { int x = k - list.size(), y = sum - str.length(); if (x == 0 && y == 0) { int j = 0; java.io.PrintWriter pw = new java.io.PrintWriter(System.out); for (int next : list) { pw.println(str.substring(j, j + next)); j += next; } pw.close(); System.exit(0); } else if (x > 0 && b * x + y >= 0 && a * x + y <= 0) { for (int i = a; i <= b; ++i) { if (sum + i > str.length()) { break; } list.push(i); f(sum + i); list.pop(); } } } }