当前位置:网站首页>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;
}
边栏推荐
- Connection and data reading of hand-held vibrating wire vh501tc collector sensor
- CCF 201503-3 节日
- MLX90640 红外热成像传感器测温模块开发笔记(三)
- Swift creates weather app
- 【Tensorflow2安装】Tensorflow2.3-CPU安装避坑指南!!!
- ISP图像信号处理
- First knowledge of opencv4.x --- image histogram equalization
- ADC introduction
- 无线振弦采集仪的使用常见问题
- 单目深度估计自监督模型Featdepth解读(下)——openMMLab框架使用
猜你喜欢

FPGA basic advanced
![[deep learning] self encoder](/img/7e/c3229b489ec72ba5d527f6a00ace01.png)
[deep learning] self encoder

无线振弦采集仪参数配置工具的设置

T5论文总结
![[Android studio] batch data import to Android local database](/img/fc/758df0ba1c4c5b4f0eb8ccbcb4952c.png)
[Android studio] batch data import to Android local database

First knowledge of opencv4.x --- image histogram matching

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

Solve the problem that esp8266 cannot connect mobile phones and computer hotspots

低功耗和UPF介绍

MLX90640 红外热成像仪测温模块开发笔记(四)
随机推荐
Verdi 基础介绍
C函数不加括号的教训
VCS常用命令
无线振弦采集仪的使用常见问题
ADC简介
dp-851
手持振弦采集仪对振弦传感器激励方法和激励电压
Store to-do items locally (improve on to-do items)
CCF 201503-3 节日
CCF 201509-2 日期计算
Advanced introduction to digital IC Design SOC
AI模型风险评估 第1部分:动机
Segmentation based deep learning approach for surface defect detection
【建议收藏】靠着这些学习方法,我入职了世界五百强——互联网时代的“奇技淫巧”
CCF 201509-4 高速公路
rospy Odometry天坑小计
FLASH read / write operation and flash upload file of esp8266
Mlx90640 infrared thermal imaging sensor temperature measurement module development notes (III)
Eco introduction
NLM5系列无线振弦传感采集仪的工作模式及休眠模式下状态