02: Find the first character that only appears once

02: Find the first character that appears only once.
View and submit statistical questions.
Total time limit: 1000ms Memory limit: 65536kB
Description
Given a string containing only lowercase letters, please find the first character that appears only once. If not, output no.

Enter
a character string less than 100000 in length.
Output
Output the first character that appears only once, if not, output no.
Sample input
abcabd
sample output
c

#include<stdio.h>
#include<string.h>

int main() 
{
    
    
	int i=0,j=0;
	char a[100001];//保存 
	int length=0;//记录长度 
	int c[10001]= {
    
    0};//记录比较长度 
	scanf("%s",a);

	length=strlen(a);
	for(i=0; i<length; i++)
	{
    
    
		//abcabd
		//abcabd
		for(j=0; j<length; j++) 
		{
    
    
			if(a[i]!=a[j])
				c[i]++;
		}
		if(c[i]==length-1) 
		{
    
    
			printf("%c",a[i]);
			return 0;
		}
	}
	printf("no");
	return 0;
}

Here is my own explanation (please forgive me if there is a mistake)
for(j=0; j<length; j++)
{ if(a[i]!=a[j]) c[i]++; } if(c[ i] == length-1) // can calculate very clever about { the printf ( "% C", A [I]); return 0; } // abcabd // abcabd ------ length = 6 wherein the first 0 pass: aa ab ac aa ab ad then c[0]=4 first pass: ba bb bc ba bb bd then c[1]=4 second pass: ca cb cc ca cb cd then c[2]=5 The third pass: aa ab ac aa ab ad then c[3]=4 The fourth pass: ba bb bc ba bb



















bd then c[4]=4
the 5th pass:
da db dc da db dd then c[5]=5
you will find that if you can output this character from c[i]=length-1, then jump out, just The latter will not be output, otherwise the output no is
reproduced at: https://www.cnblogs.com/voldemorte/p/7157058.html (If the author does not allow reprinting, please send a private message or comment, I will delete this article, thank you )

Guess you like

Origin blog.csdn.net/qq_45774073/article/details/113097747