【编程题目 |100分】字符串分割【2022 Q1, Q2考试题】


时间限制:C/C++ 1秒,其他语言 2秒

空间限制:C/C++262144K,其他语言524288K

64bit IO Format:%lld


本题可使用本地IDE编码,不能使用本地已有代码,无跳出限制,

编码后请点击”保存并调试“按钮进行代码提交。


题目描述

给定一个非空字符串S,其被N个‘-’分隔成N+1的子串,给定正整数K,要求除第一个子串外,其余的子串每K个字符组成新的子串,并用‘-’分隔。
对于新组成的每一个子串,如果它含有的小写字母比大写字母多,则将这个子串的所有大写字母转换为小写字母;
反之,如果它含有的大写字母比小写字母多,则将这个子串的所有小写字母转换为大写字母;大小写字母的数量相等时,不做转换。

输入描述

输入为两行,第一行为参数K,第二行为字符串S。

输出描述

输出转换后的字符串。

示例1  输入输出示例仅供调试,后台判题数据一般不包含示例

输入

3
12abc-abCABc-4aB@

输出

12abc-abc-ABC-4aB-@

说明

子串为12abc、abCABc、4aB@,第一个子串保留,

后面的子串每3个字符一组为abC、ABc、4aB、@,

abC中小写字母较多,转换为abc,

ABc中大写字母较多,转换为ABC,

4aB中大小写字母都为1个,不做转换,

@中没有字母,连起来即12abc-abc-ABC-4aB-@

示例2  输入输出示例仅供调试,后台判题数据一般不包含示例

输入

12
12abc-abCABc-4aB@

输出

12abc-abCABc4aB@

说明

子串为12abc、abCABc、4aB@,第一个子串保留,

后面的子串每12个字符一组为abCABc4aB@,

这个子串中大小写字母都为4个,不做转换,

连起来即12abc-abCABc4aB@


代码实现


C++


Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <bits/stdc++.h>
using namespace std;
vector<string> arrays;
void getstrings(string str)
{
istringstream ss(str);
string word;
while (ss >> word) {
arrays.push_back(word);
}
for (size_t i = 0; i < arrays.size(); i++) {
if (i) cout << ' ';
cout << arrays[i];
}
}
class Solution {
public:
string StringSpilt(int k, const string &str)
{
int pos = str.find_first_of('-');
#include <bits/stdc++.h> using namespace std; vector<string> arrays; void getstrings(string str) { istringstream ss(str); string word; while (ss >> word) { arrays.push_back(word); } for (size_t i = 0; i < arrays.size(); i++) { if (i) cout << ' '; cout << arrays[i]; } } class Solution { public: string StringSpilt(int k, const string &str) { int pos = str.find_first_of('-');
#include <bits/stdc++.h>

using namespace std;
vector<string> arrays;

void getstrings(string str)
{
    istringstream ss(str);
    string word;
    while (ss >> word) {
        arrays.push_back(word);
    }
    for (size_t i = 0; i < arrays.size(); i++) {
        if (i) cout << ' ';
        cout << arrays[i];
    }
}

class Solution {
public:
    string StringSpilt(int k, const string &str)
    {
        int pos = str.find_first_of('-');

剩余50%内容,订阅会员后查看


隐藏内容

此处内容需要权限查看

  • 普通用户特权:11金币
  • 会员用户特权:免费
  • 永久会员用户特权:免费推荐
会员免费查看

JAVA解法一


Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int count = Integer.parseInt(sc.nextLine());
String[] split = sc.nextLine().split("-");
List<String> listStr = new ArrayList<>();
StringBuilder temp = new StringBuilder();
for (int i = 1; i < split.length; i++) {
temp.append(split[i]);
}
public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int count = Integer.parseInt(sc.nextLine()); String[] split = sc.nextLine().split("-"); List<String> listStr = new ArrayList<>(); StringBuilder temp = new StringBuilder(); for (int i = 1; i < split.length; i++) { temp.append(split[i]); }
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int count = Integer.parseInt(sc.nextLine());
        String[] split = sc.nextLine().split("-");
        List<String> listStr = new ArrayList<>();
        StringBuilder temp = new StringBuilder();
        for (int i = 1; i < split.length; i++) {
            temp.append(split[i]);
        }

剩余50%内容,订阅会员后查看


隐藏内容

此处内容需要权限查看

  • 普通用户特权:11金币
  • 会员用户特权:免费
  • 永久会员用户特权:免费推荐
会员免费查看

JAVA解法二


Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.nextLine();
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); sc.nextLine();
import java.util.*;

public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        sc.nextLine();

剩余50%内容,订阅会员后查看


隐藏内容

此处内容需要权限查看

  • 普通用户特权:11金币
  • 会员用户特权:免费
  • 永久会员用户特权:免费推荐
会员免费查看


JavaScript


剩余50%内容,订阅会员后查看


隐藏内容

此处内容需要权限查看

  • 普通用户特权:11金币
  • 会员用户特权:免费
  • 永久会员用户特权:免费推荐
会员免费查看