删除字符串中字符最少字符
标题:删除字符串中字符最少字符 | 时间限制:1秒 | 内存限制:262144K | 语言限制:不限
删除字符串中出现次数最少的字符,如果有多个字符出现次数一样,则都删除。
#include <stdio.h>
#include <string.h>
int main()
{
char str[1000] = {0};
char out[1000] = {0};
int dp[1000] = {0};
int minnum = 0;
int i = 0;
int j = 0;
int k = 0;
int len = 0;
scanf("%s",str);
len = strlen(str);
minnum = len;
for(i = 0;i<len;i++)
{
for(j = 0;j<len;j++)
{
if(str[i] == str[j])
{
dp[i]++;//计数
}
}
minnum = minnum<dp[i]?minnum:dp[i];//找最小
}
for(i = 0;i<len;i++)
{
if(dp[i] != minnum)
{
out[k++] = str[i];
}
}
if(strlen(out) == 0)
{
printf("empty\n");
}
else
{
printf(out);
}
}
#include<stdlib.h>
#include<stdio.h>
#define MAX_CHAR_NUMBER 1000
int main(){
char input[MAX_CHAR_NUMBER];
scanf("%s",input);
int length=strlen(input);
int* count;
count=(int *)calloc(26,sizeof(int));
for(int i=0;i<length;i++){
int order=input[i]-'a';
count[order]++;
}
int min=MAX_CHAR_NUMBER;
for(int i=0;i<26;i++){
if(count[i]<min&&count[i]!=0){
min=count[i];
}
}
char* output;
output=(char *)calloc(length,sizeof(char));
int j=0;
for(int i=0;i<length;i++){
int order=input[i]-'a';
int count_num=count[order];
if(count_num!=min){
output[j]=input[i];
j++;
}
}
if(j==0){
printf("empty");
}else{
printf("%s",output);
}
return 0;
}
#include <iostream>
#include <unordered_map>
using namespace std;
int main()
{
string str = "";
cin >> str;
unordered_map<char, int> tem;
for(int i = 0; i < str.size(); i ++)
tem[str[i]]++;
int x = -1;
for(const auto& it : tem)
{
if(x == -1)
x = it.second;
else
x = x > it.second ? it.second : x;
}
if(x > -1)
{
string a = "";
for(int i = 0; i < str.size(); i ++)
{
if(tem[str[i]] == x)
continue;
a += str[i];
}
if(a == "")
a = "empty";
cout << a;
}
else
{
cout << "empty";
}
return 0;
}
#include<iostream>
#include<map>
#include<vector>
using namespace std;
int main(){
string in;
while(cin>>in){
map<char,int>all;
for(int i=0;i<in.length();++i){
all[in[i]]++;
}
int min=all[in[0]];
for(auto i:all){
if(i.second<min)min=i.second;
}
string res="";
for(int i=0;i<in.length();++i){
if(all[in[i]]!=min)res+=in[i];
}
if(res.length()==0)cout<<"empty"<<endl;
else cout<<res<<endl;
}
return 0;
}
