试题编号: | 201509-3 |
试题名称: | 模板生成系统 |
时间限制: | 1.0s |
内存限制: | 256.0MB |
问题描述: | 问题描述 成成最近在搭建一个网站,其中一些页面的部分内容来自数据库中不同的数据记录,但是页面的基本结构是相同的。例如,对于展示用户信息的页面,当用户为 Tom 时,网页的源代码是 输入格式 输入的第一行包含两个整数 m, n,分别表示模板的行数和模板生成时给出的变量个数。 输出格式 输出包含若干行,表示模板生成的结果。 样例输入 11 2 样例输出 <!DOCTYPE html> 评测用例规模与约定 0 ≤ m ≤ 100 |
java:
import java.util.Scanner;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), m = Integer.parseInt(sc.nextLine().trim());
String[] template = new String[n];
for (int i = 0; i < n; i++) {
template[i] = sc.nextLine();
}
Map<String, String> values = new HashMap<String, String>();
for (int i = 0; i < m; i++) {
String key = sc.next();
String value = sc.nextLine().trim();
value = value.substring(1, value.length() - 1);
values.put(key, value);
}
sc.close();
Map<String, String> second = new HashMap<String, String>();
for (int i = 0; i < n; i++) {
String str = template[i];
for(Map.Entry<String, String> e: values.entrySet()) {
if (e.getValue().matches(".*\\{{2}\\s.+\\s\\}{2}.*")) {
String secondKey = "wendy" + 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}", "");
for(Map.Entry<String, String> e2: second.entrySet()) {
str = str.replace(e2.getKey(), e2.getValue());
}
System.out.println(str);
}
}
}
c++:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int n,m,i,j,start,end,flag;
string inStr[102],outStr[102],varStr[102][2],tempVar;
cin>>n>>m;
getchar();
for(i=0;i<n;i++)
getline(cin,inStr[i]);
for(i=0;i<m;i++)
{
cin>>varStr[i][0];
getline(cin,varStr[i][1]);
start=varStr[i][1].find("\"");
end=varStr[i][1].rfind("\"");
varStr[i][1]=varStr[i][1].substr(start+1,end-start-1);
}
for(i=0;i<n;i++)
{
while(1)
{
flag=0;
start=inStr[i].find("{{ ");
end=inStr[i].find(" }}");
if(start<0||end<0) break;
tempVar=inStr[i].substr(start+3,end-start-3);
for(j=0;j<m;j++)
{
if(tempVar==varStr[j][0])
{
flag=1;
tempVar=varStr[j][1];
break;
}
}
if(flag==0) tempVar="";
outStr[i]=outStr[i]+inStr[i].substr(0,start)+tempVar;
inStr[i]=inStr[i].substr(end+3,inStr[i].length()-end-3);
}
outStr[i]=outStr[i]+inStr[i];
cout<<outStr[i]<<endl;
}
return 0;
}