0

0

翻译:对于M个查询,反转给定字符串的范围

王林

王林

发布时间:2023-08-25 20:09:09

|

1284人浏览过

|

来源于tutorialspoint

转载

翻译:对于m个查询,反转给定字符串的范围

In this problem, we will perform M reverse queries on the given string according to the array values.

The naïve approach to solving the problem is to reverse each string segment according to the given array value.

The optimized approach uses the logic that when we reverse the same substring two times, we get the original string.

Problem statement − We have given an alpha string containing the alphabetical characters. Also, we have given an arr[] array of size M containing the positive integers. We need to perform the M operations on the given string and return the final string.

In each operation, we need to take the arr[i] and reveres the substring arr[i] to N − arr[i] + 1.

示例例子

输入

alpha = "pqrst"; arr = {2, 1};

输出

tqrsp

Explanation 

  • 执行第一个查询后,字符串变为 'psrqt'。

  • 执行第二个查询后,我们得到了 'tqrsp'。

输入

−  alpha = "pqrst"; arr = {1, 1};

输出

 − ‘pqrst’

Explanation − 如果我们对同一个查询执行偶数次,我们会得到相同的字符串。

输入

 −  alpha = "pqrst"; arr = {1, 1, 1};

输出

 − ‘tsrqp’

Explanation − If we perform the same query for an odd number of times, we get the reverse of the string.

Approach 1

In this approach, we will use the reverse() method to reverse the substring. We will take the starting and ending pointers using the given query and reverse the substring of the given string.

Algorithm

步骤 1 - 开始遍历查询数组。

第2步 - 使用arr[p] - 1初始化'left'变量。

Step 3 − Initialize the ‘right’ variable with str_len − arr[p] + 1.

Mintlify
Mintlify

帮助开发者创建和维护文档

下载

Step 4 − Use the reverse() method to reverse the substring from left pointer to right pointer.

Example

#include 
using namespace std;

void reverseStrings(string &alpha, int str_len, vector &arr, int arr_len){
    // Traverse all queries
    for (int p = 0; p < arr_len; p++){
        // Get starting pointer
        int left = arr[p] - 1;
        // Ending pointer
        int right = str_len - arr[p] + 1;
        // Reverse the string
        reverse(alpha.begin() + left, alpha.begin() + right);
    }
}
int main(){
    int str_len = 5;
    string alpha = "pqrst";
    int arr_len = 2;
    vector arr = {2, 1};
    reverseStrings(alpha, str_len, arr, arr_len);
    cout << "The string after performing queries is " << alpha << endl;
    return 0;
}

输出

The string after performing queries is tqrsp

Time complexity − O(N*M) for reversing substring M times.

Space complexity − O(1) as we don’t use any dynamic space.

方法二

In this approach, we will calculate that particular index and how many times included in the reversal using given queries. If any index is included for an even number of times, we don’t need to reverse it. If any index is included for an odd number of times in all given queries, we need to reverse the character at particular indexes.

Algorithm

步骤 1 - 初始化长度等于字符串长度的 'cnt' 列表,用 0 存储特定索引在反转中出现的次数。

Step 2 − Traverse the array of given queries, and take a left and right pointer of the string according to the current query.

Step 3 − Also execute the changeRange() function to update the ‘cnt’ list according to the current query's left and right pointers.

Step 3.1 − In the changeRange() function, increment the value at the ‘left’ index in the ‘cnt’ list.

第3.2步 - 减小“cnt”列表中位于“right + 1”指针右侧的所有值。

Here, we needed to increment all the values of the ‘cnt’ list by 1 in the range [left, right]. So, we incremented only cnt[left] by 1 because taking the prefix sum will increment all values by 1, which is at right to the ‘left’ index. Also, we don’t want to increment the cnt values between [right, str_len] indexes, so we have decremented it by 1 already as the prefix sum will increase it by 1.

Step 4 − Next, execute the getPrefixSum() function to calculate the prefix sum of the ‘cnt’ list.

Step 4.1 − In the getPrefixSum() function, traverse the string and add the previous element’s value to the current element.

步骤 5 - 接下来,以逆序遍历‘cnt’列表。如果当前元素是奇数,则将其追加到‘tmp’字符串中。

步骤 6 - 用0初始化‘p’和‘q’,按照原始顺序遍历‘cnt’列表。

步骤 7 − 如果‘cnt’列表中的当前元素是奇数,则使用tmp[q]更新alpha[p]。

Step 8 − At the end, return the alpha string.

Example

的中文翻译为:

示例

#include 
#include 
using namespace std;

void changeRange(vector& cnt, int left, int right) {
    // Increase the value of the left index
    cnt[left]++;
    // Decrement value for all indexes after the right index
    if (right + 1 < cnt.size())
        cnt[right + 1]--;
}
void getPrefixSum(vector& cnt) {
    // Calculate prefix sum
    for (int p = 1; p < cnt.size(); p++) {
        cnt[p] += cnt[p - 1];
    }
}
string reverseStrings(string alpha, int str_len, vector& arr, int arr_len) {
    vector cnt(str_len, 0);
    // Traverse the array
    for (int p = 0; p < arr_len; p++) {
        int left = arr[p] <= (str_len + 1) / 2 ? arr[p] - 1 : str_len - arr[p];
        int right = arr[p] <= (str_len + 1) / 2 ? str_len - arr[p] : arr[p] - 1;
        // Changes index ranges between left and right
        changeRange(cnt, left, right);
    }
    getPrefixSum(cnt);
    string tmp;
    // Store characters with the odd reversal in the reverse order in the tmp string
    for (int p = cnt.size() - 1; p >= 0; p--) {
        if (cnt[p] % 2 != 0)
            tmp.push_back(alpha[p]);
    }
    int p = 0, q = 0;
    // For even reversal, pick the character from the original string.
    // For odd reversal, pick the character from the temp string.
    for (p = 0; p < cnt.size(); p++) {
        if (cnt[p] % 2 != 0)
            alpha[p] = tmp[q++];
    }
    // Answer string
    return alpha;
}
int main() {
    int str_len = 5;
    string alpha = "pqrst";
    int arr_len = 2;
    vector arr = { 2, 1 };
    alpha = reverseStrings(alpha, str_len, arr, arr_len);
    cout << "The string after performing queries is: " <

输出

The string after performing queries is: tqrsp

Time complexity − O(M*N + N), where O(M*N) is to update the ‘cnt’ list according to the query, and O(N) is to update the given string.

空间复杂度 - 使用 'cnt' 列表为 O(N)。

在第一种方法中,我们使用了reveres()方法来执行给定字符串上的所有查询。在第二种方法中,我们使用了前缀和技术来计算特定索引在反转中出现的次数。

相关专题

更多
C++ 高级模板编程与元编程
C++ 高级模板编程与元编程

本专题深入讲解 C++ 中的高级模板编程与元编程技术,涵盖模板特化、SFINAE、模板递归、类型萃取、编译时常量与计算、C++17 的折叠表达式与变长模板参数等。通过多个实际示例,帮助开发者掌握 如何利用 C++ 模板机制编写高效、可扩展的通用代码,并提升代码的灵活性与性能。

10

2026.01.23

php远程文件教程合集
php远程文件教程合集

本专题整合了php远程文件相关教程,阅读专题下面的文章了解更多详细内容。

29

2026.01.22

PHP后端开发相关内容汇总
PHP后端开发相关内容汇总

本专题整合了PHP后端开发相关内容,阅读专题下面的文章了解更多详细内容。

21

2026.01.22

php会话教程合集
php会话教程合集

本专题整合了php会话教程相关合集,阅读专题下面的文章了解更多详细内容。

21

2026.01.22

宝塔PHP8.4相关教程汇总
宝塔PHP8.4相关教程汇总

本专题整合了宝塔PHP8.4相关教程,阅读专题下面的文章了解更多详细内容。

13

2026.01.22

PHP特殊符号教程合集
PHP特殊符号教程合集

本专题整合了PHP特殊符号相关处理方法,阅读专题下面的文章了解更多详细内容。

11

2026.01.22

PHP探针相关教程合集
PHP探针相关教程合集

本专题整合了PHP探针相关教程,阅读专题下面的文章了解更多详细内容。

8

2026.01.22

菜鸟裹裹入口以及教程汇总
菜鸟裹裹入口以及教程汇总

本专题整合了菜鸟裹裹入口地址及教程分享,阅读专题下面的文章了解更多详细内容。

55

2026.01.22

Golang 性能分析与pprof调优实战
Golang 性能分析与pprof调优实战

本专题系统讲解 Golang 应用的性能分析与调优方法,重点覆盖 pprof 的使用方式,包括 CPU、内存、阻塞与 goroutine 分析,火焰图解读,常见性能瓶颈定位思路,以及在真实项目中进行针对性优化的实践技巧。通过案例讲解,帮助开发者掌握 用数据驱动的方式持续提升 Go 程序性能与稳定性。

9

2026.01.22

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Swoft2.x速学之http api篇课程
Swoft2.x速学之http api篇课程

共16课时 | 0.9万人学习

PHP基础入门课程
PHP基础入门课程

共33课时 | 2万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号