OJ Online software training question--delete characters with the fewest occurrences in a string (intermediate)

Source: Internet
Author: User





input:output:
Delete characters with the fewest occurrences in a string

Implements the fewest occurrences of characters in a deleted string, and is deleted if multiple occurrences of the character are the same.

output delete the string after these words, The other characters in the string remain in their original order.

Topic Category: String
Difficulty: Intermediate
Scores:
Run time limit: Ten Sec
Memory Limit: MByte
Stage: Recruitment management

The string contains only lowercase English letters,  regardless of illegal input, the input string length is less than or equal to 20 bytes.

Deletes the string after the fewest occurrences of the character in the string.

Sample output:
Dd





For such a topic, the simple thing is that the input string is not more than 20 bytes in length, because a character appears at least 1 times, and the total number of characters is 256, so you can define a flag array flag[256], which is used to record the number of occurrences of each character. Set a variable min to indicate the current occurrence of the minimum number of characters, Min can be initialized to Slen (string length, because a character can occur up to slen times), and then if there are fewer times, then min update to a smaller value, and finally from the string to the occurrence of the min characters are all deleted, Regenerate a new string and output.








#include <iostream>using namespace Std;int main () {char s[25],sout[25];cin>>s;int slen=strlen (s); int i,j=0 ; int Flag[256];memset (flag,0,sizeof (flag)); for (i=0;i<slen;i++) {flag[s[i]-' 0 ']++;} int Min=slen;for (i=0;i<256;i++) if (flag[i]<min  && flag[i]!=0) min=flag[i];for (i=0;i<slen;i++) {if (flag[s[i]-' 0 ']>min) sout[j++]=s[i];} sout[j]= ' + '; Cout<<sout<<endl;return 0;}






 Online software training topic-delete characters with the fewest occurrences in a string (intermediate)


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.