题目描述
给定一个URL前缀和URL后缀,通过”,”分割,需要将其连接为一个完整的URL,如果前缀结尾和后缀开头都没有“/”,需自动补上“/”连接符,如果前缀结尾和后缀开头都为“/”,需自动去重。
约束:不用考虑前后缀URL不合法情况。
输入描述
URL前缀(一个长度小于100的字符串),URL后缀(一个长度小于100的字符串)。
输出描述
拼接后的URL。
解题思路
- 读取输入的URL前缀和URL后缀,存储在变量 inStr 中。
- 使用 replaceFirst 方法将第一个逗号 , 替换为斜杠 /,得到临时结果 out。
- 使用 replace 方法连续两次将连续的斜杠 // 替换为单个斜杠 /,得到最终结果。
- 输出最终结果。
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;
}