首页 > 试题广场 >

删除重复字符

[编程题]删除重复字符
  • 热度指数:352 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
牛牛有一个由小写字母组成的字符串s,在s中可能有一些字母重复出现。比如在"banana"中,字母'a'和字母'n'分别出现了三次和两次。
但是牛牛不喜欢重复。对于同一个字母,他只想保留第一次出现并删除掉后面出现的字母。请帮助牛牛完成对s的操作。

输入描述:
输入包括一个字符串s,s的长度length(1 ≤ length ≤ 1000),s中的每个字符都是小写的英文字母('a' - 'z')


输出描述:
输出一个字符串,表示满足牛牛要求的字符串
示例1

输入

banana

输出

ban
#include "stdio.h"
#include "stdlib.h"

char *a(char *str)
{
    int x[128] = {0};
    int len = strlen(str);
    int i;
    // 计算出每个符号出现的次数
    for(i = 0; i < len; i++)
    {
        x[str[i]] += 1;
    }
    // 现在i 指向最后一个元素
    while(i >= 0)
    {
        if(x[str[i]] > 1)   // 表示该字符重复出现过
        {
            // 执行删除操作,将后面的字符串给复制到前面来
            // printf("%s\n", str);
            x[str[i]] -= 1;
            str[i] = 0;
            strcat(str, str + i + 1);
        }
        i--;
    }
    return str;
}


int main()
{
    char str[100000] = {0};
    fgets(str, 100000, stdin);
    char *p = str;
    p = a(str);
    printf("%s\n", p);
}

发表于 2022-09-27 23:11:34 回复(0)
import sys
string = sys.stdin.readline()
l = []
for i in string:
    if i not  in l:
        l.append(i)
sys.stdout.write("".join(l))

发表于 2020-02-01 13:21:21 回复(0)
#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;

int main(){
    int a[1000];
    memset(a,0,sizeof(a));
    char num[1000];
    gets(num);
    int j=0;
    for(int i=0;i<strlen(num);i++){
        if(a[num[i]]==0){
            a[num[i]]=1;
            num[j++]=num[i];
        }

    }
    num[j]='\0';
    puts(num);
}

发表于 2020-01-05 18:28:14 回复(0)
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    string s;
    cin >> s;
    int flag[123];
    memset(flag,0,sizeof(flag));
    int len = s.length();
    for(int i = 0;i < len;i++){
        if(flag[s[i]] == 0){
            cout << s[i];
            flag[s[i]] = 1;
        }
    }
    return 0;
}

发表于 2019-12-06 14:56:49 回复(2)
#include<iostream>
#include<string>
#include<map>
using namespace std;
int main()
{
    string strInput = "";
    cin>>strInput;
    string strResult = "";
    map<char, int> mchar;
    for(int i = 0; i < strInput.length(); i++)
    {
        if(mchar.empty())
        {
            char ptemp = strInput[i];
            mchar[ptemp] = 1;
            strResult += strInput[i];
        }
        else
        {
            char ptemp = strInput[i];
            map<char, int>::iterator it;
            it = mchar.find(ptemp);
            if(it == mchar.end())
            {
                strResult += strInput[i];
                mchar[ptemp] = 1;
            }
            else
            {
                it->second =  it->second + 1;
            }
        }
    }
    cout<<strResult<<endl;
}
发表于 2019-12-05 13:58:18 回复(0)
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        Map<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < str.length(); i++) {
            if (!map.containsKey(str.charAt(i))) {
                System.out.print(str.charAt(i));
                map.put(str.charAt(i),1);
            }
        }
        System.out.println();
    }
}

发表于 2019-12-04 21:31:56 回复(0)
s=raw_input()
res=''
forstringins :
   ifstringnotinres :
       res=res+string
print(res)

发表于 2019-12-04 19:32:05 回复(0)