试题编号: | 201509-3 |
试题名称: | 模板生成系统 |
时间限制: | 1.0s |
内存限制: | 256.0MB |
问题描述: |
问题描述 成成最近在搭建一个网站,其中一些页面的部分内容来自数据库中不同的数据记录,但是页面的基本结构是相同的。例如,对于展示用户信息的页面,当用户为 Tom 时,网页的源代码是 输入格式 输入的第一行包含两个整数 m, n,分别表示模板的行数和模板生成时给出的变量个数。 输出格式 输出包含若干行,表示模板生成的结果。 样例输入 11 2 样例输出 <!DOCTYPE html> 评测用例规模与约定 0 ≤ m ≤ 100 |
#include <iostream>
#include <map>
using namespace std;
int main()
{
int m,n;
cin>>m>>n;
getchar();
int i,j;
string str[105];
for(i=0;i<m;i++)
{
getline(cin,str[i]);//读入所有行
}
map<string,string> mp;
string s[105],t[105];
for(j=0;j<n;j++)
{
cin>>s[j];//读入变量
getchar();
getline(cin,t[j]);//读入替换变量的值
mp[s[j]]=t[j];//赋值
}
for(i=0;i<m;i++)
{
int flag=-1;
int k=0;
char temp[105];//将文中的变量读入
int len=str[i].length();
for(j=0;j<len;j++)
{
if(str[i][j]=='{'&&str[i][j+1]=='{'&&flag==-1)//判断是否找到变量
{
flag=j;
j=j+2;
}
else if(flag!=-1&&str[i][j]!=' ')
{
temp[k]=str[i][j];
k++;
}
else if(flag!=-1&&str[i][j]==' ')
{
temp[k++]='\0';
string b;
int mp_l;
b=mp[temp];//b为变量temp所应替换的值
mp_l=b.length();
if(mp_l>=1)
{
for(int l=1;l<mp_l-1;l++)//temp为{{}}之间的内容含空格
{
cout<<b[l];
}
}
flag=-1;
k=0;
j=j+2;
}
else
{
cout<<str[i][j];
}
}
cout<<endl;
}
return 0;
}