SOJ 1180. Pasting Strings

结合样例和下面图中的几种情况考虑即可解决。

代码如下:

 1 #include <iostream>
 2 #include <stack>
 3 #include <string>
 4 using namespace std;
 5
 6 int main() {
 7     int begin, end;
 8     while (cin >> begin >> end, begin != -1 && end != -1) {
 9         string text;
10         cin.get(); getline(cin, text);
11         stack<string> prepend, append;
12         for (int cur = 0; cur < begin; ++cur) {
13             if (text[cur] == ‘<‘) {
14                 if (text[cur + 1] == ‘/‘) {
15                     prepend.pop(); append.pop();
16                 } else {
17                     int tail = cur + 1;
18                     while (text[tail] != ‘>‘) ++tail;
19                     string tag = text.substr(cur, tail - cur + 1);
20                     prepend.push(tag);
21                     append.push(tag.insert(1, "/"));
22                 }
23             }
24         }
25         for (int cur = begin; cur < end; ++cur) {
26             if (text[cur] == ‘<‘) {
27                 if (text[cur + 1] == ‘/‘) {
28                     append.pop();
29                 } else {
30                     int tail = cur + 1;
31                     while (text[tail] != ‘>‘) ++tail;
32                     string tag = text.substr(cur, tail - cur + 1);
33                     append.push(tag.insert(1, "/"));
34                 }
35             }
36         }
37         string ans = text.substr(begin, end - begin);
38         while (!prepend.empty()) {
39             ans = prepend.top() + ans; prepend.pop();
40         }
41         while (!append.empty()) {
42             ans += append.top(); append.pop();
43         }
44         cout << ans << endl;
45     }
46     return 0;
47 }
时间: 2024-11-05 21:27:21

SOJ 1180. Pasting Strings的相关文章

编程题目分类(剪辑)

1. 编程入门 2. 数据结构 3. 字符串 4. 排序 5. 图遍历 6. 图算法 7. 搜索:剪枝,启发式搜索 8. 动态规划/递推 9. 分治/递归 10. 贪心 11. 模拟 12. 算术与代数 13. 组合问题 14. 数论 15. 网格,几何,计算几何 [编程入门] PC 110101, uva 100, The 3n+1 problem, 难度 1 PC 110102, uva 10189, Minesweeper, 难度 1 PC 110103, uva 10137, The T

(转)sicily题目分类

Sicily题目分类 ·         [数据结构/图论] 1310 Right-Heavy Tree   笛卡尔树相关,复杂度O(N)或O(NlogN). ·1426 Phone List         电话号码前缀检索,trie树相关. ·1443 Printer Queue      基本队列操作. ·1149 等价表达式         判断表达式是否等价(递归求解) ·1136 山海经             n长序列里求m次区间询问的最大连续子区间和.线段树/RMQ ·1252

Soj题目分类

-----------------------------最优化问题------------------------------------- ----------------------常规动态规划  SOJ1162 I-Keyboard  SOJ1685 Chopsticks SOJ1679 Gangsters SOJ2096 Maximum Submatrix  SOJ2111 littleken bg SOJ2142 Cow Exhibition  SOJ2505 The County

HOJ 题目分类

转自:http://blog.sina.com.cn/s/blog_65f3869301011a1o.html ******************************************************************************* 简单题(包括枚举,二分查找,(复杂)模拟,基础数据结构(栈.队列),杂题等 ****************************************************************************

43. Multiply Strings

1. 问题描述 Given two numbers represented as strings, return multiplication of the numbers as a string.Note:The numbers can be arbitrarily large and are non-negative.Converting the input string to integer is NOT allowed.You should NOT use internal librar

Multiply Strings

package cn.edu.xidian.sselab;/** * title:Multiply Strings * content: * Given two numbers represented as strings, return multiplication of the numbers as a string. * Note: The numbers can be arbitrarily large and are non-negative. * 读已知条件可以,两个String转换

leetcode笔记:Isomorphic Strings

一.题目描述 Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with another character while preserving the order of chara

LeetCode:Isomorphic Strings

1.题目名称 Isomorphic Strings(同构的字符串) 2.题目地址 https://leetcode.com/problems/isomorphic-strings/ 3.题目内容 英文: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurre

poj 2406 Power Strings KMP

Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative inte