













Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Discussion slides from a university of michigan class focusing on input parsing and tokenizing techniques for extracting information from delimited strings. The slides cover various methods using string library functions, iostream get/getline functionality, and stringstream. The document also introduces the concept of anytime algorithms for real-time or limited-time computation.
Typology: Assignments
1 / 21
This page cannot be seen from the preview
Don't miss anything!














z^
−^
−^
−^
−^
−^
−^
z^
The C strtok() function
char str[] = “1,2,3,40,5,12” ;char *tmp;printf ("Splitting string "%s" into tokens:\n",str);tmp = strtok (str,",");while (tmp != NULL) {
printf ("%s\n",tmp);tmp = strtok (NULL, ","); }
z^
Use string library functions^ string tmp;string delims = “ ,”;int j = 0;int i = s.find_first_not_of(delims,j);while (i != string::npos) {
j = s.find(delims, i);tmp = s.substr(i, j-i);I = s.find_first_not_of(delims,j);// process tmp }
z^
Use a stringstream (if the tokens are spaces)
int i;stringstream a(“1 2 3 40 5 12”)while (a>>i) {
// process i }
double getcputime(void) {
struct timeval tmpTime;struct rusage ru;getrusage(RUSAGE_SELF, &ru);tmpTime = ru.ru_utime;double t = (double)tim.tv_sec + (double)tim.tv_usec / 1000000.0;tim = ru.ru_stime;t += (double)tim.tv_sec + (double)tim.tv_usec / 1000000.0;return t; }
z^
In many applications, we need an answer, even if it is not the bestanswer^ −
Available time may be unknown − Solution times can vary
widely
for different instances
z^
We want an algorithm that will always return the best answer it can, giventhe time we allow it^ −
“Real time” or “anytime” computation
z^
For PA3, you must return the best solution you can on 60 seconds. Howcould you approach this?
z^
Always store the best known solution to return when time is up (and foruse as a bound) z^
First, compute quick solutions that may not be very good^ −
Random − Heuristic solution (e.g. greedy) − 2-approximation z^
Local search for improvements (k-opt) z^
Compute the optimal solution (BnB)