zoukankan      html  css  js  c++  java
  • 去除重复字符并排序

    这里有两种方法,一个是把输入的一串字符进行排序,然后输出不重复的字符;
    二是用一个长度为256的数组进行标记,有点像hash表的方式!!

    1
    #include <iostream> 2 3 #include <string> 4 5 using namespace std; 6 7 void sorthash(string ptr ,int length) 8 { 9 char hash[256] = { 0 }; 10 int i = 0; 11 for (i = 0; i < length; i++) 12 { 13 if (0 == hash[ptr[i]]) 14 { 15 hash[ptr[i]] = 1; 16 } 17 } 18 for (i = 0; i < 256; i++) 19 { 20 if (hash[i] == 1) 21 { 22 cout << (char)i; 23 } 24 25 } 26 27 } 28 void sort(string str1,int length)//冒泡 29 { 30 char temp; 31 for (int j = 0; j < length - 1; j++) 32 { 33 for (int i = 0; i < length - 1 - j; i++) 34 { 35 if (str1[i]>str1[i + 1]) 36 { 37 temp = str1[i]; 38 str1[i] = str1[i + 1]; 39 str1[i + 1] = temp; 40 } 41 } 42 } 43 cout << str1[0]; 44 for (int i = 0; i < length - 1; i++) 45 { 46 47 if (str1[i + 1] != str1[i]) 48 { 49 cout<<str1[i + 1]; 50 } 51 } 52 } 53 54 55 int main(void) 56 { 57 string str = " "; 58 int len = 0; 59 60 cin >> str; 61 len = str.length(); 62 sort(str,len); 63 //sorthash(str, len); 64 return 0; 65 }
  • 相关阅读:
    凸包模板
    1060E Sergey and Subway(思维题,dfs)
    1060D Social Circles(贪心)
    D
    牛客国庆集训派对Day2
    网络流
    Tarjan算法(缩点)
    莫队分块算法
    计算几何
    hdu5943素数间隙与二分匹配
  • 原文地址:https://www.cnblogs.com/hhboboy/p/5325119.html
Copyright © 2011-2022 走看看