From 1199b187daa65b762d2f6b681d54e9aac2780783 Mon Sep 17 00:00:00 2001 From: pjungeblut Date: Tue, 25 Nov 2014 20:57:41 +0100 Subject: Eingabehilfe für Zeilenweise-Eingabe MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- convenience/convenience.tex | 4 ++++ convenience/split.cpp | 9 +++++++++ sonstiges/sonstiges.tex | 2 +- tcr.pdf | Bin 202167 -> 203167 bytes tcr.tex | 1 + 5 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 convenience/convenience.tex create mode 100644 convenience/split.cpp diff --git a/convenience/convenience.tex b/convenience/convenience.tex new file mode 100644 index 0000000..7871efd --- /dev/null +++ b/convenience/convenience.tex @@ -0,0 +1,4 @@ +\section{Convenience-Methoden} + +\subsection{Zeileneingabe} +\lstinputlisting{convenience/split.cpp} \ No newline at end of file diff --git a/convenience/split.cpp b/convenience/split.cpp new file mode 100644 index 0000000..d7628f3 --- /dev/null +++ b/convenience/split.cpp @@ -0,0 +1,9 @@ +vector split(string &s, string delim) { //zerlegt s anhand aller Zeichen in delim + vector result; char *token; + token = strtok((char*)s.c_str(), (char*)delim.c_str()); + while (token != NULL) { + result.push_back(string(token)); + token = strtok(NULL, (char*)delim.c_str()); + } + return result; +} \ No newline at end of file diff --git a/sonstiges/sonstiges.tex b/sonstiges/sonstiges.tex index a0e2fb5..2cfd9d7 100644 --- a/sonstiges/sonstiges.tex +++ b/sonstiges/sonstiges.tex @@ -15,7 +15,7 @@ Wenn die Eingabe aus einem kleinen Intervall $\left[0, n\right)$ stammt ist Buck \lstinputlisting{sonstiges/bucketSort.cpp} \subsubsection{LSD-Radixsort} -\lstinputlisting{sonstiges/radixsort.cpp} +\lstinputlisting{sonstiges/radixSort.cpp} \subsection{Bit Operations} \lstinputlisting{sonstiges/bitOps.cpp} diff --git a/tcr.pdf b/tcr.pdf index e13c314..628d51c 100644 Binary files a/tcr.pdf and b/tcr.pdf differ diff --git a/tcr.tex b/tcr.tex index cbdb2e6..2d9fc82 100644 --- a/tcr.tex +++ b/tcr.tex @@ -70,5 +70,6 @@ \input{string/string} \input{java/java.tex} \input{sonstiges/sonstiges} +\input{convenience/convenience} \end{document} \ No newline at end of file -- cgit v1.2.3 From 3e4cca763aad161d84e524502605239487ccbf8e Mon Sep 17 00:00:00 2001 From: pjungeblut Date: Tue, 25 Nov 2014 22:18:16 +0100 Subject: bitonic tsp --- graph/bitonicTSP.cpp | 13 +++++++++++++ graph/graph.tex | 3 +++ tcr.pdf | Bin 203167 -> 204181 bytes 3 files changed, 16 insertions(+) create mode 100644 graph/bitonicTSP.cpp diff --git a/graph/bitonicTSP.cpp b/graph/bitonicTSP.cpp new file mode 100644 index 0000000..4e4dda0 --- /dev/null +++ b/graph/bitonicTSP.cpp @@ -0,0 +1,13 @@ +vector< vector > dp; //initialize with -1 +vector< vector > dist; //initialize with all dists between points +vector lr, rl; //left-to-right and right-to-left paths +int n; //number of points +double get(int p1, int p2) { //call get(0, 0) to get length of shortest bitonic route + int v = max(p1, p2) + 1; + if (v == n - 1) return dist[p1][v] + dist[v][p2]; + if (dp[p1][p2] > -0.5) return dp[p1][p2]; + double tryLR = dist[p1][v] + get(v, p2), tryRl = dist[v][p2] + get(p1, v); + if (tryLR < tryRL) lr.push_back(v); //reconstructs the path, pushes v to rl if the choice does not matter + else rl.push_back(v); //change this if needed + return min(tryLR, tryRL); +} \ No newline at end of file diff --git a/graph/graph.tex b/graph/graph.tex index 98d5ffd..a80f502 100644 --- a/graph/graph.tex +++ b/graph/graph.tex @@ -69,3 +69,6 @@ Finde die maximale Anzahl Pfade von $s$ nach $t$, die keinen Knoten teilen. \subsection{Maximal Cardinatlity Bipartite Mathcing} \lstinputlisting{graph/maxCarBiMatch.cpp} +\subsection{Bitonic TSP} +\lstinputlisting{graph/bitonicTSP.cpp} + diff --git a/tcr.pdf b/tcr.pdf index 628d51c..0778bd6 100644 Binary files a/tcr.pdf and b/tcr.pdf differ -- cgit v1.2.3 From ac2c261f2c662f021c1016f18998a621c3ba0eaa Mon Sep 17 00:00:00 2001 From: pjungeblut Date: Tue, 25 Nov 2014 22:54:57 +0100 Subject: bits --- sonstiges/bitOps.cpp | 23 ++++++++--------------- tcr.pdf | Bin 204181 -> 204053 bytes 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/sonstiges/bitOps.cpp b/sonstiges/bitOps.cpp index 20ca532..b882965 100644 --- a/sonstiges/bitOps.cpp +++ b/sonstiges/bitOps.cpp @@ -1,22 +1,15 @@ -// [lsb: 0-th bit, msb: n-th bit,] - -//Get j-th bit: +//lsb: 0-th bit, msb: n-th bit +//get j-th bit (a & (1 << j)) != 0 - -//Set j-th bit: +//set j-th bit a |= (1 << j) - -//Clear j-th bit: +//clear j-th bit a &= ~(1 << j) - -//Toggle j-th bit: +//toggle j-th bit a ^= (1 << j) - -//Get value of first set bit: +//get value of least significant bit set (a & -a) - -//Turn on all bits: +//turn on all bits a = -1 - -//Turn on first n bits: +//turn on first n bits (be aware of overflows) a = (1 << n) - 1 diff --git a/tcr.pdf b/tcr.pdf index 0778bd6..06664a8 100644 Binary files a/tcr.pdf and b/tcr.pdf differ -- cgit v1.2.3 From 1580486eda52a91b206691e5cfff5b8460f2206e Mon Sep 17 00:00:00 2001 From: pjungeblut Date: Tue, 25 Nov 2014 23:38:01 +0100 Subject: Josephus Code --- java/java.tex | 11 +++++++++-- sonstiges/josephus2.cpp | 8 ++++++++ sonstiges/josephusK.cpp | 4 ++++ sonstiges/sonstiges.tex | 2 ++ tcr.pdf | Bin 204053 -> 205970 bytes 5 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 sonstiges/josephus2.cpp create mode 100644 sonstiges/josephusK.cpp diff --git a/java/java.tex b/java/java.tex index 469fcc1..e8e132c 100644 --- a/java/java.tex +++ b/java/java.tex @@ -1,18 +1,25 @@ \section{Java} +\lstset{language=Java} \subsection{Introduction} \begin{itemize} \item Compilen: \lstinline{javac main.java} \item Ausführen: \lstinline{java main < sample.in} -\item Einlesen: -\lstset{language=Java} +\item Eingabe: \begin{lstlisting} Scanner in = new Scanner(System.in); //java.util.Scanner String line = in.nextLine(); //reads the next line of the input int num = in.nextInt(); //reads the next token of the input as an int double num2 = in.nextDouble(); //reads the next token of the input as a double \end{lstlisting} +\item Ausgabe: +\begin{lstlisting} +//Ausgabe in StringBuilder schreiben und am Ende alles auf einmal ausgeben -> viel schneller +StringBuilder sb = new StringBuilder(); //java.lang.StringBuilder +sb.append("Hallo Welt"); +System.out.print(sb.toString()); +\end{lstlisting} \end{itemize} \subsection{BigInteger} diff --git a/sonstiges/josephus2.cpp b/sonstiges/josephus2.cpp new file mode 100644 index 0000000..7676e3c --- /dev/null +++ b/sonstiges/josephus2.cpp @@ -0,0 +1,8 @@ +int rotateLeft(int n) { //returns the number of the last survivor (1 based) + for (int i = 31; i >= 0; i--) + if (n & (1 << i)) { + n &= ~(1 << i); + break; + } + n <<= 1; n++; return n; +} \ No newline at end of file diff --git a/sonstiges/josephusK.cpp b/sonstiges/josephusK.cpp new file mode 100644 index 0000000..e3fcac2 --- /dev/null +++ b/sonstiges/josephusK.cpp @@ -0,0 +1,4 @@ +int josephus(int n, int k) { //returns the number of the last survivor (0 based) + if (n == 1) return 0; + return (josephus(n - 1, k) + k) % n; +} \ No newline at end of file diff --git a/sonstiges/sonstiges.tex b/sonstiges/sonstiges.tex index 2cfd9d7..255af6a 100644 --- a/sonstiges/sonstiges.tex +++ b/sonstiges/sonstiges.tex @@ -27,6 +27,8 @@ Wenn die Eingabe aus einem kleinen Intervall $\left[0, n\right)$ stammt ist Buck $n$ Personen im Kreis, jeder $k$-te wird erschossen. \begin{description} \item[Spezialfall $k=2$:] Betrachte Binärdarstellung von $n$. Für $n = 1b_1b_2b_3..b_n$ ist $b_1b_2b_3..b_n1$ die Position des letzten Überlebenden. (Rotiere $n$ um eine Stelle nach links) + \lstinputlisting{sonstiges/josephus2.cpp} \item[Allgemein:] Sei $F(n,k)$ die Position des letzten Überlebenden. Nummeriere die Personen mit $0, 1, \ldots, n-1$. Nach Erschießen der $k$-ten Person, hat der Kreis noch Größe $n-1$ und die Position des Überlebenden ist jetzt $F(n-1,k)$. Also: $F(n,k) = (F(n-1,k)+k)\%n$. Basisfall: $F(1,k) = 0$. + \lstinputlisting{sonstiges/josephusK.cpp} \end{description} \textbf{Beachte bei der Ausgabe, dass die Personen im ersten Fall von $1, \ldots, n$ nummeriert sind, im zweiten Fall von $0, \ldots, n-1$!} diff --git a/tcr.pdf b/tcr.pdf index 06664a8..b014dc2 100644 Binary files a/tcr.pdf and b/tcr.pdf differ -- cgit v1.2.3 From 2cc01237f578c21941aa70b908a2662d443f8c1a Mon Sep 17 00:00:00 2001 From: JBatzill Date: Wed, 26 Nov 2014 00:22:51 +0100 Subject: Create TSP.cpp --- graph/TSP.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 graph/TSP.cpp diff --git a/graph/TSP.cpp b/graph/TSP.cpp new file mode 100644 index 0000000..c38cbb2 --- /dev/null +++ b/graph/TSP.cpp @@ -0,0 +1,28 @@ +//nodes[0] has to be the start and end node. +vector> dist; +vector TSP() { + int n = dist.size(), m = 1 << n; + vector> dp(n, vector(m, ii(MAX_N, -1))); + + for(int c = 0; c < n; c++) dp[c][m-1].first = dist[c][0], dp[c][m-1].second = 0; + + for(int v = m - 2; v >= 0; v--) { + for(int c = n - 1; c >= 0; c--) { + for(int g = 0; g < n; g++) { + if(g != c && (((1 << g) & v) == 0)) { + if((dp[g][(v | (1 << g))].first + dist[c][g]) < dp[c][v].first) { + dp[c][v].first = dp[g][(v | (1 << g))].first + dist[c][g]; + dp[c][v].second = g; + } + } + } + } + } + + vector res; res.push_back(0); int v = 0; + while(res.back() != 0 || res.size() == 1) { + res.push_back(dp[res.back()][(v |= (1 << res.back()))].second); + } + + return res; +} -- cgit v1.2.3 From 24507ee02a8fe20fb22ae30642e3b15e411e82eb Mon Sep 17 00:00:00 2001 From: JBatzill Date: Wed, 26 Nov 2014 00:23:27 +0100 Subject: Update graph.tex --- graph/graph.tex | 3 +++ 1 file changed, 3 insertions(+) diff --git a/graph/graph.tex b/graph/graph.tex index a80f502..6c4b1e8 100644 --- a/graph/graph.tex +++ b/graph/graph.tex @@ -69,6 +69,9 @@ Finde die maximale Anzahl Pfade von $s$ nach $t$, die keinen Knoten teilen. \subsection{Maximal Cardinatlity Bipartite Mathcing} \lstinputlisting{graph/maxCarBiMatch.cpp} +\subsection{TSP} +\lstinputlisting{graph/TSP.cpp} + \subsection{Bitonic TSP} \lstinputlisting{graph/bitonicTSP.cpp} -- cgit v1.2.3