华为OD机试 - 拼接URL(Java & JS & Python & C & C++)

题目描述

给定一个URL前缀和URL后缀,通过”,”分割,需要将其连接为一个完整的URL,如果前缀结尾和后缀开头都没有“/”,需自动补上“/”连接符,如果前缀结尾和后缀开头都为“/”,需自动去重。

约束:不用考虑前后缀URL不合法情况。

输入描述

URL前缀(一个长度小于100的字符串),URL后缀(一个长度小于100的字符串)。

输出描述

拼接后的URL。

解题思路

  1. 读取输入的URL前缀和URL后缀,存储在变量 inStr 中。
  2. 使用 replaceFirst 方法将第一个逗号 , 替换为斜杠 /,得到临时结果 out。
  3. 使用 replace 方法连续两次将连续的斜杠 // 替换为单个斜杠 /,得到最终结果。
  4. 输出最终结果。

Java算法源码

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    while (in.hasNextLine()) {
        String inStr = in.nextLine();
        String out =  inStr.replaceFirst(",", "/");
        out = out.replace("//", "/");
        out = out.replace("//", "/");
        System.out.println(out);
    }
}

JavaScript算法源码

// 引入readline模块  
const readline = require('readline');  
  
// 创建readline接口实例  
const rl = readline.createInterface({  
    input: process.stdin,  
    output: process.stdout  
});  
  
// 定义一个函数来处理输入字符串  
function processInput(inputString) {  
    // 使用正则表达式替换第一个逗号为斜杠  
    let out = inputString.replace(/,/, '/', 1);  
  
    // 使用循环来替换连续的斜杠  
    while (out.includes('//')) {  
        out = out.replace('//', '/');  
    }  
  
    // 输出处理后的字符串  
    console.log(out);  
}  
  
// 读取用户输入,直到EOF(Ctrl+D或Ctrl+Z+Enter)  
rl.on('line', (input) => {  
    processInput(input);  
});

Python算法源码

# 导入input函数,用于获取用户输入  
from sys import stdin  
  
# 主函数  
def main():  
    # 创建一个迭代器,用于逐行读取标准输入  
    for in_str in stdin:  
        # 去除输入字符串末尾的换行符  
        in_str = in_str.strip()  
          
        # 替换第一个逗号为斜杠  
        out = in_str.replace(",", "/", 1)  
          
        # 替换连续的斜杠为单个斜杠  
        while "//" in out:  
            out = out.replace("//", "/")  
          
        # 输出结果  
        print(out)  
  
# 调用主函数  
if __name__ == "__main__":  
    main()

C算法源码

#include <stdio.h>  
#include <string.h>  
#include <stdlib.h>  
  
// 函数声明  
char* replace_first(char* str, char old_char, char new_char);  
char* remove_double_slashes(char* str);  
  
int main(int argc, char *argv[]) {  
    char input[1024]; // 假设输入行不会超过1023个字符  
    char *output;  
      
    // 循环读取输入直到EOF  
    while (fgets(input, sizeof(input), stdin) != NULL) {  
        // 去除换行符  
        input[strcspn(input, "\n")] = 0;  
          
        // 替换第一个逗号为斜杠  
        output = replace_first(input, ',', '/');  
          
        // 去除连续的斜杠  
        output = remove_double_slashes(output);  
          
        // 输出结果  
        printf("%s\n", output);  
          
        // 释放之前分配的内存  
        free(output);  
    }  
      
    return 0;  
}  
  
// 替换字符串中第一个出现的指定字符  
char* replace_first(char* str, char old_char, char new_char) {  
    char *result = malloc(strlen(str) + 1); // 分配内存  
    if (result == NULL) {  
        perror("Memory allocation failed");  
        exit(EXIT_FAILURE);  
    }  
    char *pos = strchr(str, old_char); // 查找第一个出现的字符  
    if (pos == NULL) {  
        strcpy(result, str); // 如果没有找到,直接复制原字符串  
    } else {  
        // 复制直到找到旧字符的位置  
        strncpy(result, str, pos - str);  
        result[pos - str] = new_char; // 替换字符  
        strcat(result, pos + 1); // 追加剩余部分  
    }  
    return result;  
}  
  
// 去除字符串中连续的斜杠  
char* remove_double_slashes(char* str) {  
    char *output = strdup(str); // 复制原字符串  
    if (output == NULL) {  
        perror("Memory allocation failed");  
        exit(EXIT_FAILURE);  
    }  
    char *pos = output;  
    while (*pos != '\0') {  
        if (*pos == '/' && *(pos + 1) == '/') {  
            // 跳过第二个斜杠  
            memmove(pos + 1, pos + 2, strlen(pos + 2) + 1);  
        } else {  
            pos++;  
        }  
    }  
    return output;  
}

C++算法源码

#include <iostream>  
#include <string>  
#include <sstream>  
  
using namespace std;  
  
int main() {  
    // 创建一个输入流对象,用于从标准输入读取数据  
    stringstream ss;  
    string line;  
  
    // 循环读取每一行输入,直到没有更多输入为止  
    while (getline(cin, line)) {  
        // 读取到的输入行  
        string inStr = line;  
        // 替换输入行中的第一个逗号为斜杠  
        string out = inStr.substr(0, inStr.find(",", 0)) + "/" + inStr.substr(inStr.find(",", 0) + 1);  
          
        // 替换连续的两个斜杠为一个斜杠(由于已经替换过,此操作可能多余)  
        size_t pos = 0;  
        while ((pos = out.find("//", pos)) != string::npos) {  
            out.replace(pos, 2, "/");  
            pos += 1; // 移动位置以避开刚替换的斜杠  
        }  
  
        // 输出替换后的字符串  
        cout << out << endl;  
    }  
  
    return 0;  
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值