It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
here is a general implementation
int stridx (char[] src, char[] str){
int i,j,k;
for(i=0;i < (src.len - str.len);i++){
for(j=i,k=0; str[k] != '\0' && str[k] == src[i]; j++,k++);
if( k> 0 && str[k]=='\0') return i;
}
return -1;
}
The worst case of the algorithm could be n^2, if we have aaaaaaaaaaaaaaaaaaaaaaaa (assuming both src and str are very long, and the length of them is very close).
Can I have a better algorithm?
You could use the Boyer-Moore algorithm, which is O(n). Here's sample C code.
It's not necessary to calculate the length of the strings :
char *strr(char *s1,char *s2)
{
int i,j;
for(i=0;s1[i];i++)
if(s1[i]==s2[0])
for(j=0;s1[i+j]==s2[j];j++)
if(!s2[j+1]) return &s1[i];
return NULL;
}
http://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm is another linear time algorithm. OTOH, the simple algorithms stay because in practise they are faster for most of the strings people actually search with.
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
What is the most efficient way of checking if 2 strings (represented by const char *) are anagrams or not? I know we can sort and then compare. But, sorting is nlogn.
Thanks for the help.
EDIT: I got a vote down for not showing my attempt. So, my attempt is following:
int anagram(const char * c1, const char *c2){
char *s1=my_sort(c1);
char *s2=my_sort(c2);
return strcmp(s1,s2)==0?1:0;
}
It is from one of my blog posts :)
/**
* Works for 0-127 ASCII string
**/
int isanagram(const char* s1,const char* s2){
int hash[128];
int i;
for(i=0;i<128;i++)
hash[i]=0;
while(*s1) hash[*s1++]++;
while(*s2) hash[*s2++]--;
for(i=0;i<128;i++)
if(hash[i]) return 0;
return 1;
}
Explanation: Every char in the alphabet has a position in the hash table. For each char in s1 we increment the count for that char and for each char in s2 we decrement the count for the char in the hash table. if all of the char has 0 count at the end then both s1 and s2 have same number of each char, which is the definition of anagram.
Complexity: O(n) if n>128 , where n is the max of length of s1 and s2
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I need to Write a function in C language whose output with respect to input should like this:
The above table is just for an example. The input is not limited to 25, and also the number of inputs in a particular range is X instead of 5. I cannot figure out how to do this?
Right now I don't have enough time write a better question ;). Please edit it if you found any mistake.
int f(int x, int X){
return (x + (X-1))/X;
}
int func(int x)
{
if(x%5 == 0)
return x/5;
else
return x/5 + 1;
}
What about an array of structs along
struct range {
int lo, hi, result;
}
Ask the user for X, then allocate an array with X instances of this struct,
#include <stdlib.h>
struct range *array = malloc (X * sizeof *array);
Now loop over X table rows asking for the lo, hi and result. The rest is left as an exercise...
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Original page: http://acm.whu.edu.cn/learn/problem/detail?problem_id=1036
This should be a simple Dynamic Programming problem. I figured out the solution to be the following:
int main(void)
{
double d[501];
int i;
d[0] = d[1] = 1.;
d[2] = 2.;
for(i = 3; i<=500; i++)
d[i] = d[i-1] + d[i-2] + d[i-3];
int n;
while(scanf("%d", &n) == 1) {
if(n == 0) return 0;
printf("%.0lf\n", d[n]);
}
return 0;
}
But Wrong Answer reported after submission. I really don't know why.
double is not enough for the precision
you should use high-precision to solve it
Decimal point maybe. printf("%.0lf\n", 1.0); will print 1.0 but system may wait for 1.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
My order is defined as 'A < a < B < b ...< Z < z'.
I have to find if a given string is in range or not.
Ex. If my range is AaA - BaB, AA or AaaB is inthe range, but not CbAA.
I am looking for any pointers, ideas, suggestions to help me start. I will implement this in C.
So all you need to implement is a single function that compares two strings according to your rules. It is kind of modified lexicograogical sorting:
int compare_letters(char x, char y) {
char lx = tolower(x);
char ly = tolower(y);
if (lx != ly) {
return lx < ly;
} else {
return x < y;
}
}
int smaller(const char* a, const char* b) {
.. use the above function ...
}
Now make use of the above function and to check if a given string x is in the range (a,b), check if smaller(a, x) and smaller(x, b). That's it.
Some tips on the function smaller - compare the strings char by char and if the two chars differ, return their compare_letter. If one of the strings runs out of letters, consider it smaller.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
Not counting the function signature (just the body) can anybody produce C code shorter than this function that will reverse a string and return the result as a pointer to the reversed string.. (not using a string reverse library function either)?
char * reverse_str(char * s)
{
char c,*f=s,*p=s;while(*p)p++;while(--p>s){c=*p;*p=*s;*s++=c;}return f;
}
not much longer, but it works.
#include <string.h>
/* precondition: s!=const && s!=NULL && *s!='\0' */
char *mystrrev(char *s)
{
char *a=s,*e=s+strlen(s)-1;
while( a<e )
{
char c=*a;
*a++=*e;
*e--=c;
}
return s;
}