Skip to content

Commit

Permalink
Codechef tangled strings and to show all files in a directory
Browse files Browse the repository at this point in the history
  • Loading branch information
ash committed Jul 28, 2013
0 parents commit 149c8c4
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
68 changes: 68 additions & 0 deletions codechef1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#define max 5000
using namespace std;

void findsubstr(int i, string str1, string str2)
{
//string str="abab";
int a = str1.size();
int j =0;
//int <vector> result;
int total=0;
while(i<(a-j+1))
{
string temp = str1.substr (j,i);
// cout<<temp<<endl;


int positions=0; // holds all the positions that sub occurs within str

size_t pos = str2.find(temp, 0);


while(pos != string::npos)
{
positions++;
pos = str2.find(temp,pos+1);
}
total = total + positions;

//cout<<positions<<endl;

j++;
}
cout<<total<<" ";

}

int main()
{


int t,l;
cin>>t;
string str1;
string str2;
while(t--)
{
cin>>str1;
cin>>str2;
cin>>l;
int a = str1.size();
int b = str2.size();

for(int i = 1;i<=l;i++)
{
findsubstr(i,str1, str2);

}
cout<<endl;

}
/* code */
return 0;
}
37 changes: 37 additions & 0 deletions q1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>

char *path_cat (const char *str1, char *str2);

int main () {
struct dirent *dp;

// enter existing path to directory below
const char *dir_path="testing/";
DIR *dir = opendir(dir_path);
while ((dp=readdir(dir)) != NULL) {
char *tmp;
tmp = path_cat(dir_path, dp->d_name);
printf("%s\n", tmp);
free(tmp);
tmp=NULL;
}
closedir(dir);
return 0;
}

char *path_cat (const char *str1, char *str2) {
size_t str1_len = strlen(str1);
size_t str2_len = strlen(str2);
char *result;
result = (char*)malloc((str1_len+str2_len+1)*sizeof(char));
strcpy (result,str1);
int i,j;
for(i=str1_len, j=0; ((i<(str1_len+str2_len)) && (j<str2_len));i++, j++) {
result[i]=str2[j];
}
result[str1_len+str2_len]='\0';
return result;
}

0 comments on commit 149c8c4

Please sign in to comment.