当前位置:网站首页>CCF 201509-3 template generation system
CCF 201509-3 template generation system
2022-07-25 09:57:00 【Tobi_ Obito】
subject
Problem description
Cheng Cheng is building a website recently , Part of the content of some of these pages comes from different data records in the database , But the basic structure of the page is the same . for example , For pages that display user information , When the user is Tom when , The source code of the web page is 
And when the user is Jerry when , The source code of the web page is 
There are many more examples of this in websites with dynamic content . In order to simplify the generation of web pages , Cheng Cheng thinks he needs to introduce a template generation system .
Templates are text that contains special tags . The template used in Chengcheng contains only one special tag , The format is { { VAR }}, among VAR It's a variable . This tag will be changed when the template is generated VAR The value of . for example , If the variable name = "Tom", be { { name }} Will generate Tom. The specific rules are as follows :
· Variable names are made up of uppercase and lowercase letters 、 Numbers and underscores (_) constitute , And the first character is not a number , Length not exceeding 16 Characters .
· Variable names are case sensitive ,Name and name It's two different variables .
· The value of a variable is a string .
· If the variable in the tag is not defined , Then an empty string will be generated , It's equivalent to removing the tag from the template .
· Templates are not generated recursively . in other words , If the value of a variable contains a shape like { { VAR }} The content of , No further replacement .
Input format
The first line of input contains two integers m, n, It represents the number of lines of the template and the number of variables given when the template is generated .
Next m That's ok , Each line is a string , Presentation template .
Next n That's ok , Each line represents a variable and its value , Separated by a space . The value is a string , Use double quotes (") Cover up , Content can contain any printable... Except for double quotes ASCII character (ASCII Code range 32, 33, 35-126).
Output format
The output contains several lines , Represents the result of template generation .
The sample input
11 2
<!DOCTYPE html>
<html>
<head>
<title>User { { name }}</title>
</head>
<body>
<h1>{ { name }}</h1>
<p>Email: <a href="mailto:{ { email }}">{ { email }}</a></p>
<p>Address: { { address }}</p>
</body>
</html>
name "David Beckham"
email "[email protected]"
Sample output
<!DOCTYPE html>
<html>
<head>
<title>User David Beckham</title>
</head>
<body>
<h1>David Beckham</h1>
<p>Email: <a href="mailto:[email protected]">[email protected]</a></p>
<p>Address: </p>
</body>
</html>
Evaluate use case size and conventions
0 ≤ m ≤ 100
0 ≤ n ≤ 100
The length of each line of the input template does not exceed 80 Characters ( Does not contain line breaks ).
Type in all of the guarantee templates to { { The first substring is a legal token , It starts with two left braces and a space , And then the variable name , It ends with a space and two closing braces .
The value string length of all variables in the input does not exceed 100 Characters ( Do not include double quotes ).
Make sure that the names of all the variables you enter are different .
Problem analysis
This problem looks a little messy at a glance , Do not know how to start , It is a topic that needs to be analyzed systematically . First, input the template first, and then input ( Variable , value ), So you must store the template first . Besides , We can focus on each line of the template , Because the processing of each line is similar and does not affect each other . For each line , Make the following analysis :
First of all, we need location marks { { xxx }} The location of , Because the title tells us “ Type in all of the guarantee templates to { { The first substring is a legal token ”, So the starting position of the mark can be found by 2 A continuous '{' complete , Then we start from this position 、 Mark the required spaces with '}'、 Variable name length to calculate the end position of the tag '}'. In subsequent processing, you only need to replace all tags with the values of the corresponding variables .
Analyze and process each line , Let's consider the whole template . Each line may have multiple tags , So each line needs a record mark position ( The starting position , End position ) Array of , And a numeric variable to record the number of marks in each line .
Finally, the problem of mapping variables to values , This obviously works map To complete , It is worth mentioning that , Because when using map[new] And new Not before map As defined in , This will be right map[new] Default initialization , Because the mapping type in this topic is (string,string), So it will initialize by default map[new]= Empty string , In this way, there is no need to deal with the situation that there is no variable name in the tag . Don't forget to ignore the double quotation marks in the value in the details .
The easiest thing to mess up is the location of the mark , So be sure to know how the location of the mark is recorded , What I recorded was [ The starting position , End position next position ). Here's the code .
Code
#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
const int MAX = 100;
void scan(const string &s,int *num,vector<string> &vars,vector<pair<int,int> > &pos){
int i,j,len = s.length();
for(i=1;i<len-4;i++){// Legal mark {
{ At least 4 Characters "_ _ } }"
if(s[i] == '{' && s[i-1] == '{'){
*num = *num + 1;
j = i + 2;
while(s[j]!=' ') j++;
vars.push_back(string(s.begin()+i+2,s.begin()+j));
pos.push_back(make_pair(i-1,j+3));
}
}
}
void print(const string &s,int num,const vector<string> &vars,const vector<pair<int,int> > &pos,map<string,string> &f){
if(num==0){// This line is unmarked
cout<<s<<endl;
return;
}
int from = 0,to;
string t = "";
for(int i=0;i<num;i++){
to = pos[i].first;
t += string(s.begin()+from,s.begin()+to);// Connect the unmarked part to t after
t += f[string(s.begin()+pos[i].first+3, s.begin()+pos[i].second-3)];// Replace the marked part with the corresponding value of the variable , Then connect to t after
from = pos[i].second;
}
t += string(s.begin()+from,s.end());// The unmarked part at the end is connected to t after
cout<<t<<endl;
}
int main(){
int m,n;
map<string,string> f;
int row_num[MAX] = {0};// Count how many variables need to be replaced in each line
cin>>m>>n;
getchar();// Absorption enter
vector<vector<string> > vars(m);// Record the variables to be replaced in each line
vector<string> text(m);// Store the entire template
vector<vector<pair<int,int> > > pos(m);// Record the... Of each template to be replaced in each line [ Start , end ) Location
for(int i=0;i<m;i++){
getline(cin,text[i]);
scan(text[i],&row_num[i],vars[i],pos[i]);// Process each row , Find the number of tags 、 Position and record
}
string name,value,t;
string::iterator it;
for(int i=0;i<n;i++){
getline(cin,t);
it = find(t.begin(),t.end(),' ');// Find the space after the variable name , Separate variable names from values (algorithm Functions in header file )
name = string(t.begin(),it);
value = string(it+1,t.end());
f[name] = string(value.begin()+1,value.end()-1);
}
for(int i=0;i<m;i++){
print(text[i],row_num[i],vars[i],pos[i],f);// Process and output each line
}
return 0;
}
边栏推荐
- 深入理解pytorch分布式并行处理工具DDP——从工程实战中的bug说起
- Gartner 2022年顶尖科技趋势之超级自动化
- 单目深度估计自监督模型Featdepth解读(上)——论文理解和核心源码分析
- Terminal definition and wiring of bsp3 power monitor (power monitor)
- 入住阿里云MQTT物联网平台
- CDA Level1知识点总结之业务分析报告与数据可视化报表
- 基于PackNet的演进——丰田研究院(TRI)深度估计文章盘点(下)
- Connection and data reading of hand-held vibrating wire vh501tc collector sensor
- CCF 201512-3 画图
- 低功耗和UPF介绍
猜你喜欢

从鱼眼到环视到多任务王炸——盘点Valeo视觉深度估计经典文章(从FisheyeDistanceNet到OmniDet)(下)

LoRA转4G及网关中继器工作原理

单目深度估计自监督模型Featdepth解读(下)——openMMLab框架使用

First knowledge of opencv4.x --- image convolution

多通道振弦、温度、模拟传感信号采集仪数据查看和参数修改

CDA Level1知识点总结之业务分析报告与数据可视化报表

Hyperautomation for the enhancement of automation in industries 论文翻译

BSP3 电力监控仪(功率监控仪)端子定义和接线

Evolution based on packnet -- review of depth estimation articles of Toyota Research Institute (TRI) (Part 2)

Get to know opencv4.x for the first time --- add salt and pepper noise to the image
随机推荐
Introduction to arm GIC
SD/SDIO/EMMC
Mlx90640 infrared thermal imager temperature measurement module development notes (V)
CCF 201503-4 网络延时
Segmentation based deep learning approach for surface defect detection
pytorch使用tensorboard实现可视化总结
Solve the problem that esp8266 cannot connect mobile phones and computer hotspots
CDA Level1知识点总结之业务数据分析
ISP image signal processing
MLX90640 红外热成像仪测温模块开发笔记(五)
Mlx90640 infrared thermal imager temperature measurement module development notes (4)
无线振弦采集仪参数配置工具的设置
ARM预备知识
深度学习 段错误(Segment Core/ Exit code 139)情况记录
Principle analysis of self supervised depth estimation of fish eye image and interpretation of omnidet core code
工程监测多通道振弦传感器无线采集仪外接数字传感器过程
Introduction to testbench
【成长必备】我为什么推荐你写博客?愿你多年以后成为你想成为的样子。
¥ 1-1 SWUST OJ 941: implementation of consolidation operation of ordered sequence table
入住阿里云MQTT物联网平台