201509-3-template generation system

subject Problem description Chengcheng is building a website recently. Some of the contents of some pages come from dif...
subject
My train of thought
In fact, this question is not strict and can be more complicated

subject

Problem description
Chengcheng is building a website recently. Some of the contents of some pages come from different data records in the database, but the basic structure of the pages is the same. For example, for a page displaying user information, when the user is Tom, the source code of the page is

When the user is Jerry, 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 work of generating web pages, Cheng Cheng thinks he needs to introduce a template generation system.
A template is text that contains special tags. The template used in Chengcheng contains only one special tag, which is in the format of }, where VAR is a variable. The tag is replaced by the value of the VAR variable when the template is generated. For example, if the variable name = Tom, {} generates Tom. The specific rules are as follows:
· the variable name is composed of upper and lower case letters, numbers and underscores (), and the first character is not a number, with a length of no more than 16 characters.
· variable names are case sensitive, and name and name are two different variables.
· the value of the variable is a string.
· if the variable in the tag is not defined, an empty string is generated, which is equivalent to removing the tag from the template.
· templates are not generated recursively. That is to say, if the value of a variable contains something like {}, no further replacement will be made.
Input format
The first line of the input contains two integers m, n, which respectively represent the number of lines of the template and the number of variables given during template generation.
Next m lines, each of which is a string, represent the template.
Next n lines, each representing a variable and its value, separated by a space. The value is a string, enclosed in double quotation marks ("), and the contents can contain any printable ASCII characters (ASCII code range 32, 33, 35-126) except double quotation marks.
Output format
The output contains several lines representing the results generated by the template.

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>

Scale and convention of evaluation case
0 ≤ m ≤ 100
0 ≤ n ≤ 100
The length of each line of the template entered is no more than 80 characters (excluding line breaks).
The input ensures that all the substrings starting with { res=res.substr(1,res.length()-1); string key,val; string* args=new string[argsn]; while(i++!=argsn) { getline(cin,str); key=str.substr(0,str.find(" ")); args[i-1]=key; val=str.substr(str.find(" ")+1); val=val.substr(1,val.length()-2); for(int k=-1;res.find("{{",k+1)!=res.npos;) { int kr; k=res.find("{{",k+1); kr=res.find("}}",k+1)+2; if(kr!=1) str=res.substr(k+2,kr-k-4); else break; if(str==" "+key+" ") res=res.substr(0,k)+val+res.substr(kr); } } val=""; for(int k=-1;res.find("{{",k+1)!=res.npos;) { int kr; k=res.find("{{",k+1); kr=res.find("}}",k+1)+2; if(kr!=1) str=res.substr(k+2,kr-k-4); else break; for(i=0;i<argsn;i++) if(str==" "+args[i]+" ") break; if(i==argsn) res=res.substr(0,k)+val+res.substr(kr); } cout<<res; return 0; }

My train of thought

  • First, prepare a string variable to save the template, including line wrapping
  • Then, when inputting template variables and values, all template variable names are stored, and the template is converted to actual values in real time
  • After the input loop jumps out, check whether there are any template variable styles, but the strings that are not template variables (the previously stored variable names are used here) are replaced with empty strings

In fact, this question is not strict and can be more complicated

Chen &sl Published 49 original articles, won praise 0, visited 1037 Private letter follow

Tags: ascii Database

Posted on Sat, 08 Feb 2020 05:00:35 -0500 by HP400

8 February 2020, 05:00 | Views: 8594

Add new comment

For adding a comment, please log in
or create account

0 comments