问题描述
  成成最近在搭建一个网站,其中一些页面的部分内容来自数据库中不同的数据记录,但是页面的基本结构是相同的。例如,对于展示用户信息的页面,当用户为 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;
}

CCF 201509-3 模板生成系统 (STL+模拟)的更多相关文章

  1. CCF CSP 201509-3 模板生成系统

    CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201509-3 模板生成系统 问题描述 成成最近在搭建一个网站,其中一些页面的部分内容来自数据 ...

  2. CCF系列之模板生成系统( 201509-3 )

    试题名称: 模板生成系统 试题编号: 201509-3 时间限制: 1.0s 内存限制: 256.0MB 问题描述 成成最近在搭建一个网站,其中一些页面的部分内容来自数据库中不同的数据记录,但是页面的 ...

  3. CCF真题之模板生成系统

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

  4. [csp-201509-3]模板生成系统

    #include<bits/stdc++.h> using namespace std; ; string a[N],b[N],c[N]; int main() { //freopen(& ...

  5. CCF-CSP题解 201509-3 模板生成系统

    简单的替换一下字符串. 注意数组开大点. #include<bits/stdc++.h> const int maxm = 100; const int maxn = 100; using ...

  6. CCF_ 201509-3_模板生成系统

    又是一道考验细心和耐心的题,不知道哪里出问题了,一直只有90分 = =! #include<cstdio> #include<iostream> #include<cst ...

  7. ccf模板生成

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

  8. CCF 201509-3 模版生成系统

    试题编号: 201509-3 试题名称: 模板生成系统 时间限制: 1.0s 内存限制: 256.0MB 问题描述 成成最近在搭建一个网站,其中一些页面的部分内容来自数据库中不同的数据记录,但是页面的 ...

  9. ccf--20150903--模板生成系统

    本题思路:首先,使用一个map来存储所有需要替换的关键词,然后,再逐行的替换掉其中的关键词,记住,find每次的其实位置不一样,否则会出现递归生成没有出现关键词就清空掉.最后输出. 题目和代码如下: ...

随机推荐

  1. Android数据自己主动更新库DataAutoRefresh

    非常多android应用.比方音乐播放器.视频播放器.小说阅读器或者其他须要获取本地磁盘指定数据格式数据列表的应用,在磁盘数据有变化(新增或者删除.比方下载完毕,拔TF卡.换TF卡)时.须要自己主动更 ...

  2. 自动添加QQ

    自动添加QQ <meta http-equiv="refresh" content="0; url=tencent://AddContact/?fromId=50& ...

  3. JavaScript删除确认框

    1〉 <a href="javascript:if(confirm('确实要删除吗?'))location='jb51.php?id='">删除</a>

  4. 【BZOJ1042】[HAOI2008]硬币购物 容斥

    [BZOJ10492][HAOI2008]硬币购物 Description 硬币购物一共有4种硬币.面值分别为c1,c2,c3,c4.某人去商店买东西,去了tot次.每次带di枚ci硬币,买si的价值 ...

  5. EasyDarwin做转发延时太大?

    很多人反映,在用EasyDarwin做流媒体转发服务时,延时太大,实际Darwin在转发模块中,有一个控制转发Buffer时间的配置reflector_buffer_size_sec,我们将这个配置改 ...

  6. 【Effective C++】资源管理

    资源:动态分配的内存.文件描述器.互斥锁.图形界面中的字型与笔刷.数据库连接以及网络sockets等,无论哪一种资源,重要的是,当你不再使用它时,必须将它还给系统. 条款13:以对象管理资源 当我们向 ...

  7. 【网络基础系列二】BOOTP、DHCP协议

    BOOTP 含义:BOOT Protocol,引导协议 作用:引导无盘计算机或者第一次启动的计算机获取以下网络配置信息: 主机的IP地址.子网掩码 路由器(网关)的IP地址 DNS服务器IP地址 C/ ...

  8. spring+mybatis项目整合

    前辈总结的很详细,贴出链接,参考学习 http://www.open-open.com/lib/view/open1392252233301.html

  9. UVA10561 Treblecross —— SG博弈

    题目链接:https://vjudge.net/problem/UVA-10561 题意: 两个人玩游戏,轮流操作:每次往里面添加一个X,第一个得到XXX的获胜. 题解: 详情请看<训练指南&g ...

  10. nginx: error while loading shared libraries: libGeoIP.so.1

    wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP.tar.gz wget http://geolite.maxmind.com/do ...