diff --git a/Sorting/1-tutorial-intro.cpp b/Sorting/1-tutorial-intro.cpp new file mode 100644 index 0000000..046bae9 --- /dev/null +++ b/Sorting/1-tutorial-intro.cpp @@ -0,0 +1,25 @@ +#include +#include + +using namespace std; + +int main(int argc, char const *argv[]) +{ + int n, V; + cin >> V >> n; + + int temp = 0; + for (int i = 0; i < n; ++i) + { + cin >> temp; + if (temp == V) + { + cout << i << endl; + return 0; + } + } + + cout << -1 << endl; + + return 0; +} diff --git a/Sorting/1-tutorial-intro.php b/Sorting/1-tutorial-intro.php new file mode 100644 index 0000000..4afe4ad --- /dev/null +++ b/Sorting/1-tutorial-intro.php @@ -0,0 +1,8 @@ += 0; $j--) { + if ($val < $arr[$j]) { + $arr[$j + 1] = $arr[$j]; + $arr[$j] = $val; + $times ++; + } else { + break; + } + } + } + + return $times; +} + +function quickSort($arr, $start, $end) { + global $times_2; + + if ($end - $start < 2) + return; + $p = $arr[$end - 1]; + $i = $start; + for ($j=$start; $j < $end; $j++) { + if ($arr[$j] <= $p) { + $temp = $arr[$i]; + $arr[$i] = $arr[$j]; + $arr[$j] = $temp; + $times_2 ++; + $i ++; + } + } + + quickSort($arr, $start, $i - 1); + quickSort($arr, $i, $end); + + return $times_2; +} + +$handle = fopen("php://stdin", "r"); +fscanf($handle, "%d", $t); + +$arr = explode(' ', trim(fgets($handle))); + +global $times_2; +$times_2 = 0; + +echo insertionSort($arr) - quickSort($arr, 0, count($arr)) . PHP_EOL; diff --git a/Sorting/11-countingsort1.php b/Sorting/11-countingsort1.php new file mode 100644 index 0000000..c81b8e1 --- /dev/null +++ b/Sorting/11-countingsort1.php @@ -0,0 +1,22 @@ + $value) { + echo str_repeat("$key ", $value); +} diff --git a/Sorting/13-countingsort3.php b/Sorting/13-countingsort3.php new file mode 100644 index 0000000..0f80424 --- /dev/null +++ b/Sorting/13-countingsort3.php @@ -0,0 +1,20 @@ + +#include +#include + +using namespace std; + +vector a[100]; +string str; +int n, i, j, x; + +int main(int argc, char const *argv[]) +{ + cin >> n; + for (i = 0; i < n / 2; ++i) + { + cin >> x; + cin >> str; + a[x].push_back("-"); + } + + for (; i < n; ++i) + { + cin >> x; + cin >> str; + a[x].push_back(str); + } + + for (i = 0; i < 100; ++i) + { + x = a[i].size(); + for (int j = 0; j < x; ++j) + { + cout << a[i][j] << " "; + } + } + + return 0; +} diff --git a/Sorting/14-countingsort4.php b/Sorting/14-countingsort4.php new file mode 100644 index 0000000..f5aef67 --- /dev/null +++ b/Sorting/14-countingsort4.php @@ -0,0 +1,27 @@ += 0) { + echo $A[$a - $times] . PHP_EOL; + } else { + echo $A[$a - $times + $N] . PHP_EOL; + } +} diff --git a/Sorting/18-almost-sorted.cpp b/Sorting/18-almost-sorted.cpp new file mode 100644 index 0000000..fa00692 --- /dev/null +++ b/Sorting/18-almost-sorted.cpp @@ -0,0 +1,59 @@ +#include +#include +#include + +typedef long long LL; + +using namespace std; + +int main(int argc, char const *argv[]) +{ + LL n; + vector origin, sorted, diff; + cin >> n; + for (LL i = 0; i < n; ++i) { + LL x; + cin >> x; + origin.push_back(x); + sorted.push_back(x); + } + sort(sorted.begin(), sorted.end()); + + for (LL i = 0; i < n; ++i) { + if (origin[i] != sorted[i]) { + diff.push_back(i); + } + } + + LL count = diff.size(); + + if (count == 0) { + cout << "yes" << endl; + return 0; + } + + if (count == 2 && + sorted[diff[0]] == origin[diff[1]] && + sorted[diff[1]] == origin[diff[0]]) { + cout << "yes" << endl << "swap " << diff[0] + 1 << " " << diff.back() + 1; + return 0; + } + + bool reverse = false; + for (LL i = 0; i < count; i ++) { + if (origin[diff[i]] == sorted[diff[count - i - 1]]) { + reverse = true; + } else { + reverse = false; + break; + } + } + + if (reverse) { + cout << "yes" << endl << "reverse " << diff[0] + 1 << " " << diff.back() + 1; + } else { + cout << "no" << endl; + } + + return 0; +} diff --git a/Sorting/18-almost-sorted.php b/Sorting/18-almost-sorted.php new file mode 100644 index 0000000..6ca52cd --- /dev/null +++ b/Sorting/18-almost-sorted.php @@ -0,0 +1,41 @@ + 2) { + $reverse = false; + for ($i=0; $i < $count; $i++) { + if ($sorted[$diff[$i]] == $origin[$diff[$count - 1 - $i]]) { + $reverse = true; + } else { + $reverse = false; + break; + } + } + if ($reverse) { + printf("yes\nreverse %d %d\n", $diff[0] + 1, $diff[$count - 1] + 1); + } else { + echo "no\n"; + } +} diff --git a/Sorting/19-sherlock-and-pairs.php b/Sorting/19-sherlock-and-pairs.php new file mode 100644 index 0000000..22e4c36 --- /dev/null +++ b/Sorting/19-sherlock-and-pairs.php @@ -0,0 +1,27 @@ += 0; $i--) { + if ($arr[$i] > $last) { + $arr[$i + 1] = $arr[$i]; + echo implode(' ', $arr) . PHP_EOL; + } else { + $arr[$i + 1] = $last; + echo implode(' ', $arr) . PHP_EOL; + break; + } + + if ($i == 0) { + $arr[0] = $last; + echo implode(' ', $arr) . PHP_EOL; + } + } +} + +$handle = fopen("php://stdin", "r"); +fscanf($handle, "%d", $m); +$ar = explode(' ', trim(fgets($handle))); + +insertSort($ar); diff --git a/Sorting/20-insertion-sort.cpp b/Sorting/20-insertion-sort.cpp new file mode 100644 index 0000000..5057a89 --- /dev/null +++ b/Sorting/20-insertion-sort.cpp @@ -0,0 +1,68 @@ +/* Enter your code here. Read input from STDIN. Print output to STDOUT */ +#include +#include +#include +#include + +using namespace std ; + +long long cnt=0 ; + +void merge(int a[],int p,int q,int r) +{ + int l1[q-p+1] ; + int r1[r-q] ; + int n1=q-p+1 ; + int n2=r-q ; + int pos1=0,pos2=0 ; + for(int i=p;i<=q;i++) + l1[pos1++]=a[i] ; + for(int i=q+1;i<=r;i++) + r1[pos2++]=a[i] ; + int size=pos1 ; + int pos3=p ; + pos1=pos2=0 ; + while(pos1r1[pos2]){ + cnt+=size-pos1 ; + a[pos3++]=r1[pos2++] ; + }else + a[pos3++]=l1[pos1++] ; + } + if(pos1>=n1){ + while(pos2= 0; $j--) { + if ($temp < $arr[$j]) { + $arr[$j + 1] = $arr[$j]; + $arr[$j] = $temp; + } else { + break; + } + } + + echo implode(" ", $arr) . PHP_EOL; + } +} diff --git a/Sorting/4-correctness-invariant.php b/Sorting/4-correctness-invariant.php new file mode 100644 index 0000000..1291121 --- /dev/null +++ b/Sorting/4-correctness-invariant.php @@ -0,0 +1,25 @@ += 0; $j--) { + if ($val < $arr[$j]) { + $arr[$j + 1] = $arr[$j]; + $arr[$j] = $val; + } else { + break; + } + } + } +} + +$handle = fopen ("php://stdin","r"); +$t = fgets($handle); +$arr = split(' ', fgets($handle)); + +insertionSort($arr); + +foreach($arr as $value) { + print $value." "; +} diff --git a/Sorting/5-runningtime.php b/Sorting/5-runningtime.php new file mode 100644 index 0000000..b84311d --- /dev/null +++ b/Sorting/5-runningtime.php @@ -0,0 +1,27 @@ += 0; $j--) { + if ($val < $arr[$j]) { + $arr[$j + 1] = $arr[$j]; + $arr[$j] = $val; + + $times ++; + } else { + break; + } + } + } + + echo $times . PHP_EOL; +} + +$handle = fopen ("php://stdin","r"); +$t = fgets($handle); +$arr = split(' ', fgets($handle)); + +insertionSort($arr); diff --git a/Sorting/6-quicksort1.php b/Sorting/6-quicksort1.php new file mode 100644 index 0000000..338b26f --- /dev/null +++ b/Sorting/6-quicksort1.php @@ -0,0 +1,25 @@ += $equal) { + $right[] = $ar[$i]; + } else { + $left[] = $ar[$i]; + } + } + + echo implode(' ', array_merge($left, $right)) . PHP_EOL; +} + +$fp = fopen("php://stdin", "r"); + +fscanf($fp, "%d", $m); +$ar = explode(' ', trim(fgets($fp))); + +partition($ar); diff --git a/Sorting/7-quicksort2.php b/Sorting/7-quicksort2.php new file mode 100644 index 0000000..f14fa6c --- /dev/null +++ b/Sorting/7-quicksort2.php @@ -0,0 +1,41 @@ + $arr[$i]) + $left[] = $arr[$i]; + else + $right[] = $arr[$i]; + } + + if (count($left) > 1) { + $left = quickSort($left); + echo implode(' ', $left) . PHP_EOL; + } + + if (count($right) > 1) { + $right = quickSort($right); + echo implode(' ', $right) . PHP_EOL; + } + + return array_merge($left, [$temp], $right); +} + +$fp = fopen("php://stdin", "r"); + +fscanf($fp, "%d", $m); + +$ar = explode(' ', trim(fgets($fp))); + +$results = quickSort($ar); + +echo implode(' ', $results) . PHP_EOL; diff --git a/Sorting/8-quicksort3.cpp b/Sorting/8-quicksort3.cpp new file mode 100644 index 0000000..beb1005 --- /dev/null +++ b/Sorting/8-quicksort3.cpp @@ -0,0 +1,50 @@ +#include +#include +#include +#include +#include +using namespace std; + +void quicksort(vector &vec, int startIndex, int pivotIndex) +{ + int pivot, index, isgreater; + int arraySize = pivotIndex - startIndex; + pivot = vec[pivotIndex]; + + if (arraySize > 0) { + index = startIndex; + isgreater = 0; + for(int i=startIndex;i vec; + cin >> size; + for(int i = 0; i> dummy; + vec.push_back(dummy); + } + quicksort(vec, 0, size-1); + return 0; +} diff --git a/Sorting/8-quicksort3.php b/Sorting/8-quicksort3.php new file mode 100644 index 0000000..0a06715 --- /dev/null +++ b/Sorting/8-quicksort3.php @@ -0,0 +1,31 @@ + $input[$i]) { + $tmp = $input[$i]; + $input[$i] = $input[$firstHigh]; + $input[$firstHigh++] = $tmp; + } + } + $input[$end] = $input[$firstHigh]; + $input[$firstHigh] = $p; + echo implode(' ', $input) . PHP_EOL; + return $firstHigh; +} diff --git a/Sorting/9-bigger-is-greater.cpp b/Sorting/9-bigger-is-greater.cpp new file mode 100644 index 0000000..59e5ae4 --- /dev/null +++ b/Sorting/9-bigger-is-greater.cpp @@ -0,0 +1,25 @@ +#include +#include + +using namespace std; + +int main(int argc, char const *argv[]) +{ + int T; + cin >> T; + while (T --) { + string s; + cin >> s; + string prev = s; + if (next_permutation(s.begin(), s.end())) + { + cout << s << endl; + } + else + { + cout << "no answer" << endl; + } + } + + return 0; +} diff --git a/Sorting/9-bigger-is-greater.php b/Sorting/9-bigger-is-greater.php new file mode 100644 index 0000000..76cc437 --- /dev/null +++ b/Sorting/9-bigger-is-greater.php @@ -0,0 +1,48 @@ + 0 && $str[$i - 1] >= $str[$i]) { + $i --; + } + + // no nect permutation. + if ($i <= 0) return false; + + // search right greater pivot. + $j = $length - 1; + while ($str[$j] <= $str[$i - 1]) { + $j --; + } + + // change pivot and right. + $temp = $str[$i - 1]; + $str[$i - 1] = $str[$j]; + $str[$j] = $temp; + + // reverse. + $j = $length - 1; + while ($i < $j) { + $temp = $str[$i]; + $str[$i] = $str[$j]; + $str[$j] = $temp; + $i ++; + $j --; + } + + return true; +} diff --git a/Sorting/a.out b/Sorting/a.out new file mode 100755 index 0000000..7343749 Binary files /dev/null and b/Sorting/a.out differ diff --git a/string/1-pangrams.c b/String/1-pangrams.c similarity index 100% rename from string/1-pangrams.c rename to String/1-pangrams.c diff --git a/string/1-pangrams.php b/String/1-pangrams.php similarity index 100% rename from string/1-pangrams.php rename to String/1-pangrams.php diff --git a/string/10-palindrome-index.php b/String/10-palindrome-index.php similarity index 100% rename from string/10-palindrome-index.php rename to String/10-palindrome-index.php diff --git a/string/11-reverse-shuffle-merge.php b/String/11-reverse-shuffle-merge.php similarity index 100% rename from string/11-reverse-shuffle-merge.php rename to String/11-reverse-shuffle-merge.php diff --git a/string/12-the-love-letter-mystery.cpp b/String/12-the-love-letter-mystery.cpp similarity index 100% rename from string/12-the-love-letter-mystery.cpp rename to String/12-the-love-letter-mystery.cpp diff --git a/string/12-the-love-letter-mystery.php b/String/12-the-love-letter-mystery.php similarity index 100% rename from string/12-the-love-letter-mystery.php rename to String/12-the-love-letter-mystery.php diff --git a/string/13-common-child.cpp b/String/13-common-child.cpp similarity index 100% rename from string/13-common-child.cpp rename to String/13-common-child.cpp diff --git a/String/14-morgan-and-a-string.php b/String/14-morgan-and-a-string.php new file mode 100644 index 0000000..6cbfa50 --- /dev/null +++ b/String/14-morgan-and-a-string.php @@ -0,0 +1,29 @@ + +#include + +using namespace std; + +int main(int argc, char const *argv[]) +{ + // + + return 0; +} diff --git a/String/17-string-function-calculation.php b/String/17-string-function-calculation.php new file mode 100644 index 0000000..f00623d --- /dev/null +++ b/String/17-string-function-calculation.php @@ -0,0 +1,37 @@ + 1 && $current[0] == $current[1]) { + while (($pos = strpos($str, $current, $start)) !== false) { + $count ++; + $start = $pos + 1; + } + } else { + $count = substr_count($str, $current); + } + + if (($count * $i) > $max) { + $max = $count * $i; + } + } + } +} + +echo $max . PHP_EOL; diff --git a/string/2-funny-string.cpp b/String/2-funny-string.cpp similarity index 100% rename from string/2-funny-string.cpp rename to String/2-funny-string.cpp diff --git a/string/2-funny-string.php b/String/2-funny-string.php similarity index 100% rename from string/2-funny-string.php rename to String/2-funny-string.php diff --git a/String/22-string-similarity.cpp b/String/22-string-similarity.cpp new file mode 100644 index 0000000..6acefd1 --- /dev/null +++ b/String/22-string-similarity.cpp @@ -0,0 +1,124 @@ +#include +#include + +using namespace std; + +typedef long int l; + +long int z_func(const string &s) +{ + l sum = 0; + l length = s.length(); + l Z[length] = {0}; + l left = 0; + l right = 0; + for (l k = 1; k < length; ++k) + { + if (k > right) { + left = right = k; + while (right < length && s[right] == s[right - left]) { + right ++; + } + Z[k] = right - left; + right --; + } else { + int k1 = k - left; + if (Z[k1] < right - k + 1) { + Z[k] = Z[k1]; + } else { + left = k; + while (right < length && s[right] == s[right - left]) { + right ++; + } + Z[k] = right - left; + right --; + } + } + } + + for (l i = 0; i < length; ++i) + { + sum += Z[i]; + } + + return sum; +} + +int main(int argc, char const *argv[]) +{ + int T; + cin >> T; + while (T --) { + string s; + cin >> s; + l res = z_func(s); + + cout << s.length() + res << endl; + } + + return 0; +} + + +// timeout ... +// #include +// #include + +// using namespace std; + +// long int match(const string &str1, const string &str2) +// { +// if (str1.empty() || str2.empty()) { +// return 0; +// } +// long int length = str2.length(); + +// int maxSubstr = 0; +// for (long int i = 0; i < length; ++i) +// { +// if (str2[i] == str1[i]) +// maxSubstr ++; +// else +// break; +// } + +// return maxSubstr; +// } + +// long int stringSimilarity(string s) +// { +// // soso. +// long int length = s.length(); +// long int index[length]; +// long int i = 0; +// long int c= 0; +// long int count= 0; +// while (s[i] != '\0') { +// if (s[i] == s[0]) { +// index[c] = i; +// c ++; +// } +// i ++; +// } + +// for (i = 0; i < c; ++i) +// { +// string s1 = s.substr(index[i], length); +// count += match(s, s1); +// } + +// return count; +// } + +// int main(int argc, char const *argv[]) +// { +// int T; +// cin >> T; +// while (T --) { +// string s; +// cin >> s; +// cout << stringSimilarity(s) << endl; +// } + +// return 0; +// } diff --git a/String/22-string-similarity.php b/String/22-string-similarity.php new file mode 100644 index 0000000..6b4b921 --- /dev/null +++ b/String/22-string-similarity.php @@ -0,0 +1,28 @@ +