import java.util.HashMap;
import java.util.Scanner;
public class Main {
static HashMap<String,String> map = new HashMap<>();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m = sc.nextInt(),n = sc.nextInt();
sc.nextLine();
String[] str = new String[m];
for (int i = 0; i < m; i++) {
str[i] = sc.nextLine();
}
for (int i = 0; i < n; i++) {//map存入变量名变量值
String[] ss = new String[2];
ss[0] = sc.next();
ss[1] = sc.nextLine();
map.put(ss[0],ss[1].substring(2,ss[1].length() - 1));
}
for (int i = 0; i < m; i++) {
int index = str[i].indexOf("{{ ");
String pre = str[i];//用于更新找到变量后的字符串,防止递归
int last;
while (index != -1){
String befor = pre.substring(0,index);//将字符串以{{ 分为两段
System.out.print(befor);//输出前半段
String after = pre.substring(index);
last = after.indexOf(" }}");
String swa = after.substring(3,last);//找到变量名
if (map.containsKey(swa)){
System.out.print(map.get(swa));//如果map有对应的,输出变量值
}
pre = after.substring(last + 3);//取 }} 后面的字符串
index = pre.indexOf("{{ ");//继续找变量
}
System.out.println(pre);
}
}
}
CSP201509-3 模板生成系统 JAVA 38行
于 2023-08-27 17:41:00 首次发布