首页 > 试题广场 >

字符串处理函数

[编程题]字符串处理函数
  • 热度指数:297 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
现有字符串char[] text,实现函数char[] drawChar(char[] text),找出text中重复出现最多的字符,然后将该字符移到text的最前端,生成一个新的字符串。 假设重复出现最多的字符只有一个。请充分考虑内存和性能效率。 举例: “abcaba”,转换后成为“aaabcb”。


输入描述:
输入原始的字符串,如 "abcaba"


输出描述:
输入处理后的字符串,如 "aaabcb"
示例1

输入

abcaba

输出

aaabcb
#include <iostream>
usingnamespacestd;
 
 
int main(){
    inthash[256] = {0};
    string str;
    cin >> str;
    charc;
    intmax = 0;
    for(inti = 0; i < str.size(); ++i){
        ++hash[str[i] - 'a'];
        if(hash[str[i] - 'a'] > max){
            max = hash[str[i] - 'a'];
            c = str[i];
        }
    }
    inti = str.size();
    intj = str.size() - 1;
    while(j >= 0){
        if(str[j] != c){
            swap(str[--i], str[j--]);
        }else{
             --j;
        }
    }
    cout << str << endl;
     
    return0;
}

编辑于 2019-06-02 21:14:15 回复(0)
import java.util.*;

public class Main {
    // 修改字符串
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s = in.nextLine();
        Map<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            map.put(c, map.getOrDefault(c, 0)+1);
        }
        int max = 0;
        char target = ' ';
        for (char key : map.keySet()) {
            if (map.get(key) > max) {
                max = map.get(key);
                target = key;
            }
        }
        s = s.replaceAll(target+"", "");
        for (int i = 0; i < max; i++) {
            System.out.print(target);
        }
        System.out.println(s);
    }
}
发表于 2022-06-29 17:56:46 回复(0)
x =i nput()
l = list(x)
leng = 0
dif=list(set(l))
for ind,i in enumerate(dif):
    num = x.count(i)
    if num>leng:
        leng =num
        index =ind
print("".join(leng*[dif[index]]+list(x.replace(dif[index],'')))
编辑于 2019-06-28 23:27:57 回复(0)
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        char ch[] = sc.nextLine().toCharArray();
        System.out.println(drawChar(ch));
    }

    public static char[] drawChar(char[] text) {
        Map<Character, Integer> map = new HashMap();
        for (int i = 0; i < text.length; i++) {
            if (!map.containsKey(text[i])) {
                map.put(text[i], 1);
            } else {
                int val = map.get(text[i]);
                map.put(text[i], ++val);
            }
        }
        int max = 0;
        char ch = '0';
        for (Map.Entry<Character, Integer> entry : map.entrySet()) {
            if (entry.getValue() > max) {
                max = entry.getValue();
                ch = entry.getKey();
            }
        }
        StringBuffer sb_head = new StringBuffer(), sb_foot = new StringBuffer();
        for (int i = 0; i < text.length; i++) {
            if (text[i] == ch) {
                sb_head.append(text[i]);
            } else {
                sb_foot.append(text[i]);
            }
        }
        sb_head.append(sb_foot);
        return sb_head.toString().toCharArray();
    }
}
发表于 2019-03-04 21:27:20 回复(0)
#include <iostream>
#include <string>
#include <unordered_map>
#include <algorithm>

using namespace std;

inline void getResult(string& str) {
    auto len = str.size();
    unordered_map<char,int> m;
    auto max = 0;
    char byte = 0;
    for(size_t i = 0; i < len; ++i) {
        auto count = m[str[i]]++;
        if(count > max) {
            max = count;
            byte = str[i];
        }
    }
    str.erase(std::remove(str.begin(),str.end(),byte),str.end());
    std::reverse(str.begin(),str.end());
    while(max-- >= 0) {
        str.push_back(byte);
    }
    std::reverse(str.begin(),str.end());
}

int main() {
    string input;
    while(cin>>input) {
        getResult(input);
        cout<<input<<endl;
    }
    return 0;
}

发表于 2018-12-24 20:28:59 回复(0)
#include <iostream>
#include <map>
#include <math.h>
#include <string>
usingnamespacestd;
 
intmain(){
    map<char,int> a;
    ints=28,e=0;
    charch;
    map<char,int> book;
    string str="";
    cin>>str;
    intlen=str.size();
    for(intx=0;x<len;x++){
        book[str[x]]++;
        s=min(str[x]-'a',s);
        e=max(str[x]-'a',e);
    }
    intb=0;
    for(intx=s+'a';x<=e+'a';x++){
        if(b<book[x]){
            b=book[x];
            ch=x;
        }
    }
    for(intx=0;x<b;x++){
        cout<<ch;
    }
    for(intx=0;x<len;x++){
        if(str[x]!=ch){
            cout<<str[x];
        }
    }
    return0;
}

发表于 2018-12-20 12:52:31 回复(0)
# -*- coding: utf-8 -*- 
import sys 
import string 
def chartranslate():
     string0 = sys.stdin.readline()#输入
     list1=[]
     for i in string0:
         if i != '\n':
             list1.append(i)#转换成列表
     list2=list(set(list1))#转换成集合 (去重)
     count1=[]
     for j in list2:#将集合中的元素在输入数据中的个数存到count
         n=list1.count(j)
         count1.append(n) 
     m=count1.index(max(count1))#求count集合中最大值的索引  
     list3=[]   
     list4=[]  
     for k in list1:  
       if list2[m]==k:      
           list3.append(k)#输入字符串最多的重复元素        
       else:
           list4.append(k)#其他元素   
     list5=list3+list4 
     list6=''.join(list5)
     print list6 
    # print(str1)
if __name__=='__main__': 
    chartranslate()

编辑于 2018-12-18 18:57:40 回复(0)
var readline=require('readline');
var r1=readline.createInterface({
    input:process.stdin,
    output:process.stdout
});
r1.on('line',function(data){
    var num=0;
    var str="";
    var s='';
    var obj=data.split('').reduce(function(prev,curr){
        prev[curr]?prev[curr]++:prev[curr]=1;
        return prev;
    },{});
    for(var key in obj){
        if(obj[key]>num){
            num=obj[key];
            s=key;
        }
    }
    while(num>0){
        str+=s;
        num--;
    }
    console.log(str+data.replace(new RegExp(s,'g'),''));
})

发表于 2018-12-16 20:03:53 回复(0)