-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrings.cpp
More file actions
134 lines (123 loc) · 2.14 KB
/
Copy pathstrings.cpp
File metadata and controls
134 lines (123 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include<iostream>
#include<string.h>
#include<stdlib.h>
using namespace std;
void swap(char &a,char&b)
{
char t = a;
a = b;
b = t;
}
// needed for qsort
int compare(const void *a,const void *b)
{
return *(char*)a-*(char*)b;
}
void perm(char *str,int start,int end)
{
if(start == end)
cout<<str<<endl;
for(int i = start;i<=end;i++)
{
swap(str[i],str[start]);
perm(str,start+1,end);
swap(str[i],str[start]);
}
}
int find_ceil(char *str,int lower,int upper,int n)
{
int ceil_idx = upper;
for(int i=upper;i<n;i++)
if(str[i]>str[lower] && str[i]<str[upper])
ceil_idx = i;
return ceil_idx;
}
void perm_dup(char *str,int n)
{
if(n == 1)
{
cout<<str;
return;
}
// sort string
qsort(str,n,sizeof(str[0]),compare);
bool finished = false;
while(!finished)
{
cout<<str<<endl;
// find first character smaller than the next from the end
int i;
for(i=n-2;i>=0;i--)
if(str[i]<str[i+1])
break;
if(i == -1)// no more perms
{
finished = true;
break;
}
// find ceil of first char
int sec_idx = find_ceil(str,i,i+1,n);
// swap ceil char with first
swap(str[sec_idx],str[i]);
// sort after first char
qsort(str+i+1,n-i-1,sizeof(str[0]),compare);
}
}
void count_freq(char *pat,int p[],int m)
{
for(int i=0;i<m;i++)
p[pat[i]]++;
}
bool compare_freq(int a[],int b[])
{
for(int i=0;i<256;i++)
if(a[i]!=b[i])
return false;
return true;
}
void anag_subs(char *text,char *pat,int m,int n)
{
int t[256],p[256];
memset(t,0,sizeof(t));
memset(p,0,sizeof(p));
count_freq(text,t,m);
count_freq(pat,p,m);
for(int i=m;i<n;i++)
{
if(compare_freq(t,p))
cout<<"\n anagram found at "<<i-m;
// add new character
t[text[i]]++;
//remove old character
t[text[i-m]]--;
}
if(compare_freq(t,p))
cout<<"\nanagram found at "<<n-m;
}
main()
{
char str[] = {"abca"};
char text[] = {"acbaabbabca"};
int n = strlen(str),m = strlen(text);
//perm(str,0,n-1);
//perm_dup(str,n);
//anag_subs(text,str,n,m);
string a;
getline(cin,a);
int i=0,j=0;
n = a.length();
while(j<n)
{
while(a[j]==' ')
{
j++;
}
if(a[j]!=' ' && j<n)
{
a[i++] = a[j++];
}
}
a[i] = '\0';
a.erase(a.begin()+i,a.end());
cout<<a<<endl;
}