试题编号: | 201509-3 |
试题名称: | 模板生成系统 |
时间限制: | 1.0s |
内存限制: | 256.0MB |
问题描述: | 问题描述 成成最近在搭建一个网站,其中一些页面的部分内容来自数据库中不同的数据记录,但是页面的基本结构是相同的。例如,对于展示用户信息的页面,当用户为 Tom 时,网页的源代码是 |
解题代码(java):
import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(), m = Integer.parseInt(input.nextLine().trim()); String[] template = new String[n]; for (int i = 0; i < n; i++) { template[i] = input.nextLine(); } Map<String, String> values = new HashMap<String, String>(); for (int i = 0; i < m; i++) { String key = input.next(); String value = input.nextLine().trim(); value = value.substring(1, value.length() - 1); values.put(key, value); } input.close(); Map<String, String> second = new HashMap<String, String>(); for (int i = 0; i < n; i++) { String str = template[i]; Iterator<Entry<String, String>> it = values.entrySet().iterator(); while (it.hasNext()) { Entry<String, String> e = it.next(); if (e.getValue().matches(".*//{{2}//s.+//s//}{2}.*")) { String secondKey = "chenfan" + e.getKey(); str = str.replace("{{ " + e.getKey() + " }}", secondKey); second.put(secondKey, e.getValue()); } else { str = str.replace("{{ " + e.getKey() + " }}", e.getValue()); } } str = str.replaceAll("//{{2}//s.+//s//}{2}", ""); Iterator<Entry<String, String>> it2 = second.entrySet().iterator(); while (it2.hasNext()) { Entry<String, String> e2 = it2.next(); str = str.replace(e2.getKey(), e2.getValue()); } System.out.PRintln(str); } } }
新闻热点
疑难解答