-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Codechef tangled strings and to show all files in a directory
- Loading branch information
ash
committed
Jul 28, 2013
0 parents
commit 149c8c4
Showing
2 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |