poj 3768 解题报告

本文介绍了解决POJ3768模拟题的方法,通过递归深度优先搜索(DFS)策略来处理模板字符串及其重复层级,提供了一份完整的Java实现代码。

好久没做acm的题了,呵呵!最近做了一次poj的月赛,杯具,就只做起了两道题。现在发的这道题是一个模拟题poj3768,来看看这道题的题目意思吧:

就是题目给一个模板串,然后在给个重复的次数level。题意很简单。这道题就是个dfs思想,递归模拟。

我们来看看代码吧:

package poj; import java.util.Scanner; public class Main3768 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNextInt()) { int n = sc.nextInt(); if (n == 0) break; sc.nextLine(); String[] template = new String[n]; for (int i = 0; i < n; ++i) { String s = sc.nextLine(); template[i] = new String(s); } // sc.nextLine(); // sc.nextLine(); int level = sc.nextInt(); copy(template, template, level); } } private static void copy(String[] template1, String[] template2, int level) { --level; if (level > 0) { int n1 = template1.length; int n2 = template2.length; String[] tmpTemplate = new String[n1 * n2]; int k = 0; String spaceS = spaceStr(n2); for (int i = 0; i < n1; ++i) { for (int j = 0; j < n1; ++j) { k = i * n2; if (template1[i].charAt(j) == ' ') { for (int x = 0; x < n2; ++x) { if (tmpTemplate[k] != null) tmpTemplate[k] += (spaceS); else tmpTemplate[k] = new String(spaceS); k++; } } else { for (int x = 0; x < n2; ++x) { if (tmpTemplate[k] != null) tmpTemplate[k] += template2[x]; else tmpTemplate[k] = template2[x]; k++; } } } // print(tmpTemplate); } copy(template1, tmpTemplate, level); } else print(template2); } private static String spaceStr(int n) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; ++i) { sb.append(" "); } return sb.toString(); } private static void print(String[] template) { int n = template.length; // System.out.println(n); for (int i = 0; i < n; ++i) { System.out.print(template[i]); System.out.println(); } } }

http://acm.pku.edu.cn/JudgeOnline/problem?id=3768

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值