编程介的小学生 2020-01-12 14:08 采纳率: 20.5%
浏览 183

关于 Vim 编辑器

Problem Description
Vim is a text editor which developed from vi. Due to its powerful function in code complete, compile, and error jump, it’s widely used by programmers. The same as Emacs, it’s the most popular text editor among users of UNIX. As such an excellent text editor, Vim has various of orders. Now, we’re asking you to write a program that simulates Vim’s replace order.

The format of Vim’s replace order is ([] means optional, {} means necessary) :

:[range]s/{pattern}/{string}/[flag]

In the order above,

‘:’ means the start of a replace order,

[range] indicates the range of the order, that is, the order works in which lines.

‘s’ is short for substitute.

{pattern} and {string} represent the string to match and replace to, respectively.

‘/’ is used to mark the beginning and ending of {pattern} and {string}.

{flag} is used to open or close some options.

{range} is often two integers separated by a comma, indicating the start line and end line’s line number.

For example, “4,8” means from line 4 to line 8 (including line 4, line 8). Line number starts from 1. You can also use a ‘%’ to represent all lines.

(Additional, Vim provides many more flexible formats. Such as, bypassing a number means the line cursor stays, ‘′meansthelastlineofthetext.So,“,” means from the line cursor stays to the last line. )

{pattern} and {string} both support regular expression (if you haven’t ever heard of it, go to Google for help). If {pattern} is empty, the {pattern} of the last replace order will be used.

Obviously, ‘/’ can’t be included in {pattern} and {string}. So, an additional escape character ‘\’ is used. For example, if you want to replace “
” to “
”, you cannot write:

:%s/
/
/g

Instead, you should write:

:%s/
/
/g

If there’re too many ‘/’s in the expression, (for instance, "file:///usr/share/man/man1/vim.1.gz"), it will become troublesome. So, people think of a solution, that is, use another character as the separator (the first character after ‘s’ is always treated as the separator). For example, when using ‘+’ as the separator, the order above can be written this way:

:%s+
+
+g

There’re many kinds of [flag]. ‘g’ means replace every time it matches. Without a ‘g’, it will only replace the first matching string. For example:

#include

Execute the order bellow:

:%s/i//g

Result is:

#nclude

While executing this order bellow:

:%s/i//

Result is:

#nclude

Other flags including: ‘c’ indicates a confirm is required before every replacement, ‘i’ indicates case insensitive.

Here comes a problem, what if you want to replace “a” to “aa”? Somebody may doubt that it will go to an endless loop, but in fact, it won’t. Because there is a rule that, in a replacement, the replaced characters can’t be replaced again. So, if you want to replace “a” to “aa”, it’s in fact that every successive “a” string is doubled in length.

To simplify the problem, we make some appointments:

  1. [range] must appear, in the form of “%” or “a,b” (a, b are both integers, and a<=b)

  2. {pattern} and {string} are both consist of characters, numbers, spaces and “_” (not including any separator below, so an escape character is not needed)

  3. You can choose one of these characters as the separator: /~!@#$%^&*()-+=

  4. [flag] is always a “g”

A big example:

Original text:

If the Tao is greet, then the operating system is greet.

If the operating system is greeter, then the compiler is greet.

If the compiler is greeter, then the applications is greet.

The user is pleased and there is harmony in the world.

Replace order:

:[1,4]s/greet/great/g

Or:

:%s+greet+great+g

Either will replace the original text to:

If the Tao is great, then the operating system is great.

If the operating system is greater, then the compiler is great.

If the compiler is greater, then the applications is great.

The user is pleased and there is harmony in the world.

Please write a program to simulate this simplified vim replace order.

Input
The input will consist of one case.

The first line will be a positive integer L (L <= 100), specifying the number of lines to be processed. Then L lines of text are given. Each line has no more than 100 characters.

After that, several pieces (<= 50) of replace orders are given (one per line). It is ensured that any line of text will never have more than 100 characters during the replacement.

Output
After the execution of every replace order, output the line number and content of the lines that have been replaced, order by line number from small to big. In each line, first output the line number, which has a width of 4 characters, right-aligned, then 2 spaces, then the text after replacement.

If any order replaced nothing, output "Pattern not found".

Output a blank line between any two replace order’s result.

Sample Input
4
If the Tao is greet, then the operating system is greet.
If the operating system is greeter, then the compiler is greet.
If the compiler is greeter, then the applications is greet.
The user is pleased and there is harmony in the world.
:1,3s/greet/great/g
:%s//great/g

Sample Output
1 If the Tao is great, then the operating system is great.
2 If the operating system is greater, then the compiler is great.
3 If the compiler is greater, then the applications is great.

Pattern not found

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 simulink中怎么使用solve函数?
    • ¥30 dspbuilder中使用signalcompiler时报错Error during compilation: Fitter failed,求解决办法
    • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
    • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
    • ¥15 知识蒸馏实战博客问题
    • ¥15 用PLC设计纸袋糊底机送料系统
    • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
    • ¥15 用C语言输入方程怎么
    • ¥15 网站显示不安全连接问题
    • ¥15 51单片机显示器问题