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)