Problem Description Chengcheng recently set up a Web site where some of the pages come from different data records in the database, but the basic structure of the page is the same. For example, for a page that displays user information, when the user is Tom, the source code for the page is
And when the user is Jerry, the source code of the Web page is
Examples of this are many of the sites that contain dynamic content. To simplify the work of generating web pages, Chengcheng felt he needed to introduce a set of template generation systems. A template is text that contains special tags. The template used by Chengcheng contains only a special tag in the format {{VAR}}, where VAR is a variable. The token is replaced by the value of the variable VAR when the template is generated. For example, if the variable name = "Tom", then {{name}} will generate Tom. The specific rules are as follows: • Variable names are composed of uppercase and lowercase letters, numbers, and underscores (_), and the first character is not a number and is not longer 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 a 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 recursively generated. That is, if the value of the variable contains the contents of a shape such as {{VAR}}, no further substitutions are made. Input format the first line of input contains two integers m, n, which represent the number of rows of the template and the number of variables given when the template was generated. Next m line, each line is a string representing the template. Next n rows, each row represents a variable and its value, separated by a space. The value is a string, enclosed in double quotation marks ("), which can contain any printable ASCII character except double quotes (ASCII range 32, 33, 35-126). The output format output contains several lines that represent the results of the template generation. Sample Input 2 <! DOCTYPE html> < html> < Head> < Title>user {{Name}}</title> #include <string.h>
#include <stdio.h>
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 ();///when there is a problem with input and output, try to join GetChar to handle line breaks
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++)
{
flag=0; At the beginning flag initialization here, looking for a long time bug to find is here wrong
while (1)
{
Flag=0;//flag should be defined within a loop, because a statement contains multiple variables, and flag is used to mark whether the variable is defined
Start=instr[i].find ("{{");
End=instr[i].find ("}}");
if (start<0| | end<0) break; If "{{" or "}}" is not found, exit the loop
Tempvar=instr[i].substr (start+3,end-start-3);//Remove the value of the variable, such as name
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];//don't forget to add the last remaining piece of input
cout<<outstr[i]<<endl;
}
return 0;
}
Template generation system of CCF real problem