JXNVCE ALGO-LOG YouJin Jung

GFG ANAGRAM SUBSTRING SEARCH

#include<iostream>
#include<cstring>

#define MAX 256
using namespace std;
 
bool compare(char arr1[], char arr2[]) {
    for (int i=0; i<MAX; i++)
        if (arr1[i] != arr2[i])
            return false;
    return true;
}
 
void search(char *pat, char *txt) {
    int M = strlen(pat)
    int N = strlen(txt);
    char countP[MAX] = {0}, countTW[MAX] = {0};
    
    for (int i = 0; i < M; i++) {
        (countP[pat[i]])++;
        (countTW[txt[i]])++;
    }
    for (int i = M; i < N; i++) {
        if (compare(countP, countTW))
            cout << "Found at Index " << (i - M) << endl;
        (countTW[txt[i]])++;
        countTW[txt[i-M]]--;
    }
 
    if (compare(countP, countTW))
        cout << "Found at Index " << (N - M) << endl;
}
 
int main() {
    char txt[] = "BACDGABCDA";
    char pat[] = "ABCD";
    search(pat, txt);
    return 0;
}

GFG SORT IN WAVE

Sorting array in wave >= <-

#include <iostream>
#include <algorithm>

using namespace std;

void swap(int *x, int *y) {
    int temp = *x;
    *x = *y;
    *y = temp;
}

void sortInWave(int arr[], int n) {
    sort(arr, arr+n);

    for (int i=0; i<n-1; i += 2) {
        swap(&arr[i], &arr[i+1]);
    }
}
 
int main() {
    int arr[] = {10, 90, 49, 2, 1, 5, 23};
    int n = sizeof(arr)/sizeof(arr[0]);
    sortInWave(arr, n);
    for (int i=0; i<n; i++)
       cout << arr[i] << " ";
    return 0;
}

GFG ITERATIVE QUICK SORT

#include <bits/stdc++.h>
 
using namespace std;
 
void swap(int* a, int* b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}
 
int partition(int arr[], int l, int h) {
    int x = arr[h];
    int i = (l - 1);
 
    for (int j = l; j <= h - 1; j++) {
        if (arr[j] <= x) {
            i++;
            swap(&arr[i], &arr[j]);
        }
    }
    swap(&arr[i + 1], &arr[h]);
    return (i + 1);
}
 
void quickSort(int A[], int l, int h) {
    if (l < h) {
        /* Partitioning index */
        int p = partition(A, l, h);
        quickSort(A, l, p - 1);
        quickSort(A, p + 1, h);
    }
}
 
int main() {
 
    int n = 5;
    int arr[n] = { 4, 2, 6, 9, 2 };
 
    quickSort(arr, 0, n - 1);
 
    for (int i = 0; i < n; i++) {
        cout << arr[i] << " ";
    }
 
    return 0;
}

GFG BOOLEAN ARRAY PUZZLE

#include <bits/stdc++.h>

using namespace std;

void changeToZero(int a[2]) {
    a[ a[1] ] = a[ !a[1] ];
}

int main() {
    int a[] = {1, 0};
    changeToZero(a);
     
    cout << "arr[0] = " << a[0] << endl;
    cout << " arr[1] = " << a[1];
    return 0;
}

GFG NO. OF WAYS TO GET NTH STAIRS (2)

  1. 두번째 방식인 DP
#include <iostream>

using namespace std;

int countWaysUtil(int n, int m) {
    int res[n];
    res[0] = 1;
    res[1] = 1;
     
    for(int i = 2; i < n; i++) {
       res[i] = 0;
       for(int j = 1; j <= m && j <= i; j++) {
          res[i] += res[i - j];
       }
    }
    return res[n - 1];
}

int countWays(int s, int m) {
    return countWaysUtil(s + 1, m);
}

int main() {
    int s = 4, m = 2;
    cout << "Number of ways = " << countWays(s, m);         
    return 0;
}