Click to See Complete Forum and Search --> : need an algorithm
Dillinger4
Aug 19th, 2001, 08:33 PM
Does any one happen to have an algorithm to pick
only the characters that appear in a String one time?
Im trying to write one but im having a hard time.
for(int index = 0; index <= key.length() - 1; ++index){
char matchingchar = key.charAt(index);
for(int k = 1; k <= key.length() - (2 + index); ++k){
if(matchingchar == key.charAt(k)){
charmatch = true;
break;
}
}
if(charmatch == false){++onlyappearsonce;}
charmatch = false;
}
SteveCRM
Aug 19th, 2001, 09:23 PM
are you using a character array or the string header?
Dillinger4
Aug 19th, 2001, 09:28 PM
Actually im doing it in Java, but the Java fourm is dead as usual
Im not too sure what you mean by header. Im just passing a string to a function that is supposed to calculate hoe many characters appear only once within the string.
Dillinger4
Aug 19th, 2001, 10:44 PM
I came up with this and it should work. But i cant get the
if(matchingchar == key.charAt(q)) part because say for a string
"abcde" q would equal 3 then 2 then 1 so i would be compairing
matchingchar with the 3rd index which is c instead of b. not what i want. so ill have to work on this. Thanks though.
for(int index = 0; index <= key.length() - 1; ++index){
char matchingchar = key.charAt(index);
x = (key.length() * (key.length() - 2));
x =+ (key.length() - 2);
while(q != 0){
q = x % key.length();
if(matchingchar == key.charAt(q)){
++duplicates;
break;
}
--x;
}
}
int nonduplicatechars = (key.length() - (duplicates));
kedaman
Aug 19th, 2001, 11:23 PM
How long are the strings? How many types of characters are used and how are they used?
Generally I think the fastest way for not too short strings or Strings with generally few reoccurring characters, would be to set up a table of trinary values for each character, and then run trough the string once flagging on characters from 0(not used) to 1(used once) and from 1 to 2(used more than once). Then count the 1's in the table.
Dillinger4
Aug 20th, 2001, 02:58 PM
Hey kedaman. you seen to know C++ pretty well. How could i make this expression legal? q and x are integer types.
while(q = x % key.length() != 0){
Dillinger4
Aug 20th, 2001, 03:09 PM
This is somthing that i thought might work, but maybe im just going crazy thinking im on the right track.
For the first loop matchingchar = key.charAt(index);
will give me the first character then
x = (key.length() * (key.length() - 2));
x =+ (key.length() - 2);
will give me for a string say "ABCDF"
15 = 5 * 3
18 =+ 5 - 2
then
q = x % key.length() != 0
3 = 18 % 5
2 = 17 % 5
1 = 16 % 5
that would give me the correct amount of iterations to get to the end of the string each time no matter the size of the string.
I think :p
for(int index = 0; index <= key.length() - 1; ++index){
matchingchar = key.charAt(index);
x = (key.length() * (key.length() - 2));
x =+ (key.length() - 2);
while(q = x % key.length() != 0){
if(matchingchar == key.charAt(z)){
++duplicates;
}
--x;
++z;
}
}
int nonduplicatechars = (key.length() - (duplicates));
Zaei
Aug 20th, 2001, 03:45 PM
while((q = x % key.length()) != 0) { .. }
Z.
Dillinger4
Aug 20th, 2001, 04:30 PM
Kool, ill give it a try....... Thanks
kedaman
Aug 21st, 2001, 05:06 AM
I have no idea what solution you're doing, may i even don't know what the problem is. If you fail, I could give you a hand finding the amount of duplicates
Dillinger4
Aug 21st, 2001, 02:10 PM
Thanks. If i do fail. Ill give you a yell. :D
Dillinger4
Aug 22nd, 2001, 11:24 PM
I came up with this and it seems to work pretty good but the problem i am having as far as i can see is that i have to take account not only for duplicate characters but for multiple characters (3 or more) I tried this out on words like Kaaat (not that this is a word) and the results are off. And also i have to figure out how to take into account if the multiple characters are seperated. for instance. Kaata. Right now all i am doing is subtracting 2 which takes care of the original and the duplicate.
Any ideas?
for(int index = 0; index <= key.length() - 2; ++index){
matchingchar = key.charAt(index);
x = (((key.length() - 2) + (key.length() * (key.length() - 2))) - index);
while((q = x % key.length()) != 0){
if(key.charAt(z) == matchingchar){
duplicates =+ 2;
}
--x;
++z;
}
z =+ 1;
}
int nonduplicatechars = (key.length() - duplicates);
kedaman
Aug 23rd, 2001, 01:28 AM
screw it and go with the trinary table
Dillinger4
Aug 23rd, 2001, 01:48 AM
What is that? Exactly. :confused:
kedaman
Aug 23rd, 2001, 01:55 AM
An array of 3 different values, you could use char for instance.
each value represents a character and their value represents:
0 - Not used
1 - Used once
2 - Used twice or more
Dillinger4
Aug 23rd, 2001, 01:59 AM
Is there any way i can mimic this in Java?
I havent coded in C++ in ages.
kedaman
Aug 23rd, 2001, 02:09 AM
Of course, I know some Java, but I'm rusty on the string class, so you could make an array of char's instead. I probably will type something wrong but you'll know what I mean.
char ttable[256] = new char[256]
int l = key.length();
for (int x = 0; x<l ; ++x; ){
char matchingchar = key.charAt(x);
if (ttable[matchingchar]<2) ++ttable[matchingchar];
}
for (x = 0; x<256 ; ++x; ){ char count += ttable[x] == 1;}
jim mcnamara
Aug 23rd, 2001, 05:04 AM
in C and skipping the ternary aspect, simply adding.
void blah(char *ch)
{
int arr[256];
char tmp[256];
int j = 0;
int i = 0;
for (i = 0;i < 256;i++) { arr[i] = 0; }
for (i=0;i< (int) strlen(ch);i++) {
++arr[ ch[i] ];
}
for (i=33;i<256;i++) {
if (arr[i] == 1) { tmp[j++] = i; } //33 to ignore sdpaces
}
tmp[j] = '\0'; // make it a valid sz string
}
kedaman
Aug 23rd, 2001, 05:12 AM
here again in Java
int ttable[256] = new int[256]
int l = key.length();
for (int x = 0; x<l ; ++x; )
++ttable[key.charAt(x)];
for (x = 0; x<256 ; ++x; )
char count += ttable[x] == 1; // note no conditional stuff here either
but it depends what you count as "character" so i'll leave that
Dillinger4
Aug 24th, 2001, 01:41 AM
Hey thanks guys for posting so code. Mabey you should start asking for some $$ for your help. :p Anyway..... kedaman the code that you posted, im not too sure if i understand.
int ttable[256] = new int[256]
int l = key.length();
for (int x = 0; x<l ; ++x; )
++ttable[key.charAt(x)];
for (x = 0; x<256 ; ++x; )
char count += ttable[x] == 1;
for instance ++ttable[key.charAt(x)]; you would be storing the whole string within the array. correct?
and this line char count += ttable[x] == 1; why would we be testing a char to see if it is equal to the length of the string?
kedaman
Aug 24th, 2001, 03:06 AM
No actually, from what I understood you want to count the amount of non duplicated characters, that is characters that are used only once. If that's not the case then you have to explain again.
++ttable[key.charAt(x)];
ttable is a table of all the characters from 0 to 255, each element specifying how many times it has been used. key.charAt(x) gets the current character and then that characters counter is incremented in the table.
char count += ttable[x] == 1;
here in this loop the count is incremented only when the current character count is 1, == returns 1 if the comparation is true otherways 0. see the logic? There's no need for conditional code
Dillinger4
Aug 24th, 2001, 02:30 PM
So actually each time you encounter a character you are incrementing by 1. Ok now i see. :p
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.