CCF 201509-3 模板生成系统 (STL+模拟)

问题描述
  成成最近在搭建一个网站,其中一些页面的部分内容来自数据库中不同的数据记录,但是页面的基本结构是相同的。例如,对于展示用户信息的页面,当用户为 Tom 时,网页的源代码是


  而当用户为 Jerry 时,网页的源代码是


  这样的例子在包含动态内容的网站中还有很多。为了简化生成网页的工作,成成觉得他需要引入一套模板生成系统。
  模板是包含特殊标记的文本。成成用到的模板只包含一种特殊标记,格式为 {{ VAR }},其中 VAR 是一个变量。该标记在模板生成时会被变量 VAR 的值所替代。例如,如果变量 name = "Tom",则 {{ name }} 会生成 Tom。具体的规则如下:
  ·变量名由大小写字母、数字和下划线 (_) 构成,且第一个字符不是数字,长度不超过 16 个字符。
  ·变量名是大小写敏感的,Name 和 name 是两个不同的变量。
  ·变量的值是字符串。
  ·如果标记中的变量没有定义,则生成空串,相当于把标记从模板中删除。
  ·模板不递归生成。也就是说,如果变量的值中包含形如 {{ VAR }} 的内容,不再做进一步的替换。
输入格式
  输入的第一行包含两个整数 m, n,分别表示模板的行数和模板生成时给出的变量个数。
  接下来 m 行,每行是一个字符串,表示模板。
  接下来 n 行,每行表示一个变量和它的值,中间用一个空格分隔。值是字符串,用双引号 (") 括起来,内容可包含除双引号以外的任意可打印 ASCII 字符(ASCII 码范围 32, 33, 35-126)。
输出格式
  输出包含若干行,表示模板生成的结果。
样例输入
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 "david@beckham.com"
样例输出
<!DOCTYPE html>
<html>
<head>
<title>User David Beckham</title>
</head>
<body>
<h1>David Beckham</h1>
<p>Email: <a href="mailto:david@beckham.com">david@beckham.com</a></p>
<p>Address: </p>
</body>
</html>
评测用例规模与约定
  0 ≤ m ≤ 100
  0 ≤ n ≤ 100
  输入的模板每行长度不超过 80 个字符(不包含换行符)。
  输入保证模板中所有以 {{ 开始的子串都是合法的标记,开始是两个左大括号和一个空格,然后是变量名,结尾是一个空格和两个右大括号。
  输入中所有变量的值字符串长度不超过 100 个字符(不包括双引号)。
  保证输入的所有变量的名字各不相同。
 
析:用STL模拟就好,但是只得了90分,不知道哪错了,哪位大神看出来, 告诉一下,感激不尽。
 
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e4 + 5;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
    return r >= 0 && r < n && c >= 0 && c < m;
}
vector<string> text;

int main(){
    while(scanf("%d %d", &n, &m) == 2){
        getchar();
        string line;
        text.clear();
        for(int i = 0; i < n; ++i){
            getline(cin, line);
            text.push_back(line);
        }

        int pos;
        while(m--){
            string s1, s2;
            getline(cin, line);
            pos = line.find(" ");
            s1 = line.substr(0, pos);
            s2 = line.substr(pos+2, (int)line.size()-pos-3);

            for(int i = 0; i < n; ++i){
                string &s = text[i];
                pos = 0;
                while(true){
                    int pos1 = s.find("{{ ", pos);
                    int pos2 = s.find(" }}" , pos1);
                    if(pos1 < 0 || pos2 < 0)  break;
                    string t = s.substr(pos1+3, pos2-pos1-3);
                    if(t == s1)  s.replace(pos1, pos2-pos1+3, s2, 0, s2.size());
                    pos = pos1+s2.size();
                }
            }
        }

        for(int i = 0; i < n; ++i){
            string &s = text[i];
            pos = 0;
            while(true){
                int pos1 = s.find("{{ ", pos);
                int pos2 = s.find(" }}", pos1);
                if(pos1 < 0 || pos2 < 0)  break;
                s.replace(pos1, pos2-pos1+3, "", 0, 0);
                pos = pos1;
            }
        }
        for(int i = 0; i < n; ++i)
            cout << text[i] << endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/dwtfukgv/p/5861316.html