diff options
| -rw-r--r-- | convenience/convenience.tex | 4 | ||||
| -rw-r--r-- | convenience/split.cpp | 9 | ||||
| -rw-r--r-- | graph/TSP.cpp | 28 | ||||
| -rw-r--r-- | graph/bitonicTSP.cpp | 13 | ||||
| -rw-r--r-- | graph/graph.tex | 6 | ||||
| -rw-r--r-- | java/java.tex | 11 | ||||
| -rw-r--r-- | sonstiges/bitOps.cpp | 23 | ||||
| -rw-r--r-- | sonstiges/josephus2.cpp | 8 | ||||
| -rw-r--r-- | sonstiges/josephusK.cpp | 4 | ||||
| -rw-r--r-- | sonstiges/sonstiges.tex | 4 | ||||
| -rw-r--r-- | tcr.pdf | bin | 203257 -> 209720 bytes | |||
| -rw-r--r-- | tcr.tex | 1 |
12 files changed, 93 insertions, 18 deletions
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<string> split(string &s, string delim) { //zerlegt s anhand aller Zeichen in delim + vector<string> 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/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<vector<int>> dist; +vector<int> TSP() { + int n = dist.size(), m = 1 << n; + vector<vector<ii>> dp(n, vector<ii>(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<int> 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; +} 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<double> > dp; //initialize with -1 +vector< vector<double> > dist; //initialize with all dists between points +vector<int> 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 4c49663..827892b 100644 --- a/graph/graph.tex +++ b/graph/graph.tex @@ -70,3 +70,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} + 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/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/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 a0e2fb5..255af6a 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} @@ -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$!} Binary files differ@@ -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 |
