Input_codes

Demonstrace jednoduchého načítání

Uvádíme možné způsoby načítání dat v Cpp a v Javě. Demonstrujeme na úlohách z UVA odkazovaných níže. Uvádíme pouze načítání bez výpočtu řešení úlohy.

V Cpp vystačíme s příkazy cin » promenna » …, občas se hodí k identifikaci konce vstupu funkce std::cin.eof().

V Javě vystačíme s BufferedReader-em a StringTokenizer-em.

Načítání v Pythonu lze strukturovat podle Javy, StringTokenizer zaměnit za metodu split(), příkaz input() v Pythonu odpovídá přečtení řádku v Javě pomocí myReliableReader.readLine().

Veškeré tyto metody jsou dostatečně rychlé pro řešení naprosté většiny úloh na serverech (skoro možná všech).

Cpp

#include <iostream>
#include <vector>
#include <string>
 
using namespace std;
 
-------------------------------------------------------------------------
 
void dummyLoad_UVA336(){
 
  // input values are randomly scatterd over the input lines
  // (but in correct order, of course).
  int  NC, n1, n2, q1, q2;
 
  while( true ){
    cin >> NC;
    if( NC == 0 ) break;
    //  edges
    for( int i = 0; i < NC; i++ ){
      cin >> n1 >> n2;
      // --> process edge here <--
    }
    // queries
    while( true ) {
      cin >> q1 >> q2;
      if( q1 == 0 && q2 == 0 ) break;
      // --> process query and print
      cout << "Hello!" << endl;
    }
  } // while cases exist
}
 
-------------------------------------------------------------------------
 
void dummyLoad_UVA439(){
  string pos1, pos2;
 
  while( true ){
    cin >> pos1 >> pos2;
    // --> process query and print
    cout << "Hello!" << endl;
    if( std::cin.eof() ) break;
  }
}
 
-------------------------------------------------------------------------
 
void dummyLoad_UVA459(){
  int  N;
  string maxLabel, edge;
  string line;
 
  // use getline consistently in the whole input:
  std::getline(std::cin, line);
  N = std::stoi( line );
 
  for( int i = 0; i < N; i++ ){
    std::getline(std::cin, maxLabel);
    while( true ){
      if( std::cin.eof() ) break;
      std::getline(std::cin, edge);
      if( edge.empty() ) break;
      // --> register edge;
    }
    // --> process graph and print
      cout << "Hello!" << endl;
   } // for all cases
}
 
-------------------------------------------------------------------------
 
void dummyLoad_UVA567(){
  int  NQ, listLen, neigh, q1, q2;
 
  while( true ) {
    // graph
    for( int i = 0; i < 19; i++ ){
      cin >> listLen;
      for( int j = 0; j < listLen; j++ ){
        cin >> neigh;
        // --> register neighbour
      }
    }
    cin >> NQ; // no of queries
    for( int i = 0; i < NQ; i++ ){
      cin >> q1 >> q2;
      // --> process query and print
      cout << "Hello!" << endl;
    }
    // try to find next case:
    if( std::cin.eof() ) break;
  } // while cases exist
}
 
-------------------------------------------------------------------------
 
void dummyLoad_UVA699(){
 
  // input values are randomly scatterd over the input lines
  // (but in correct order, of course).
  // strangely, in this case we HAVE to interpret the input values
  // to be able to detect the end of the input/cases correctly
 
  int  key;
  std::vector<int> stack (100000);
  int SP = -1; // Stack Pointer
 
  // get first key
  cin >> key;
  stack[++SP] = 1; // first visit
 
  while( true ){
    if( SP == -1 ){ //start reading new tree or break if at the end of input;
      cin >> key;
      if( key > 0 ) stack[++SP] = 1; // push new root
      else break;
    }
    else{ // reading inside the tree
      cin >> key;
      if( key > 0 ) stack[++SP] = 1;
      else{ // -1 read, move up the tree
        while( SP >= 0 && stack[SP] == 2 ) SP--; // third visit, move up
        if( SP >= 0 ) stack[SP]++; // second visit to a node
        else { // finished tree reading, produce output
          cout << "Hello!" << endl;
        }
      }
    }
  } // while cases exist
}
 
-------------------------------------------------------------------------
 
void dummyLoad_UVA10069(){
  int  N;
  string seq1, seq2;
 
  cin >> N;
 
  for( int i = 0; i < N; i++ ){  // cases
    cin >> seq1 >> seq2;
    // --> process sequences
    // output
    cout << "Hello!" << endl;
  }
}
 
-------------------------------------------------------------------------
 
void dummyLoad_UVA10849(){
  int  N, tests, boardSize, q1, q2, q3, q4;
 
  cin >> N;
  // "cin >> ..." skips empty line automatically
 
  for( int i = 0; i < N; i++ ){  // cases
    cin >> tests >> boardSize;
    for( int j = 0; j < N; j++ ){ // read queries
      cin >> q1 >> q2 >> q3 >> q4;
      // --> process query and output
      cout << "Hello!" << endl;
    }
  }
}
 
-------------------------------------------------------------------------
 
void dummyLoad_UVA11195(){
  int  N;
  string line;
  while( true ){
    cin >> N;
    if( N == 0 ) break;
    for( int i = 0; i < N; i++ ){ // read edges
     cin >> line;
     // --> register row
    }
    // process case and output:
    cout << "Hello!" << endl;
  }
}
 
-------------------------------------------------------------------------
 
void dummyLoad_UVA11396(){
  int  N, a, b;
  while( true ){
    cin >> N;
    if( N == 0 ) break;
    while( true ){ // read edges
     cin >> a >> b;
     if( a == 0 && b == 0 ) break;
     // --> register edge
    }
    // process case and output:
    cout << "Hello!" << endl;
  }
}
 
-------------------------------------------------------------------------
 
void dummyLoad_UVA11504(){
  int  cases, m, n, a, b;
  cin >> cases;
  for( int i = 0; i < cases; i++ ){
    cin >> n >> m;
    for( int j = 0; j < m; j++ ){ // read edges
     cin >> a >> b;
     // --> register edge
    }
    // process case and output:
    cout << "Hello!" << endl;
  }
}
 
-------------------------------------------------------------------------
 
int main() {
  dummyLoad_UVA336();
  return 0;
}

Java

import java.io.*;
import java.util.*;
 
 
public class UVA336 {
 
public static void main(String[] args) throws IOException {
  dummyLoad();
}
 
// input values are randomly scatterd over the input lines
// (but in correct order, of course).
 
// before accesing any value in the  input 
// check that your buffer (String Tokenizer in this case)
// contains something to be read and processed. 
static void dummyLoad() throws IOException {
  int  NC, n1, n2, q1, q2;
 
	BufferedReader br = new BufferedReader( new InputStreamReader(System.in) );
 
  while( true ) {
    StringTokenizer st = new StringTokenizer( br.readLine() );    
    while( !st.hasMoreTokens() ) st = new StringTokenizer( br.readLine() );    
    NC = Integer.valueOf( st.nextToken() ); 
    if ( NC == 0 ) break; 
     //  edges
    for( int i = 0; i < NC; i++ ){
      while( !st.hasMoreTokens() ) st = new StringTokenizer( br.readLine() );    
      n1 =  Integer.valueOf( st.nextToken() );
      while( !st.hasMoreTokens() ) st = new StringTokenizer( br.readLine() );    
      n2 =  Integer.valueOf( st.nextToken() );
      // --> process edge here <--  
    }
    // queries
    while( true ) {
      while( !st.hasMoreTokens() ) st = new StringTokenizer( br.readLine() );    
      q1 =  Integer.valueOf( st.nextToken() );
      while( !st.hasMoreTokens() ) st = new StringTokenizer( br.readLine() );    
      q2 =  Integer.valueOf( st.nextToken() );
      if( q1 == 0 && q2 == 0 ) break;
 
      // --> process query here <-- 
      System.out.printf( "Hello!\n" );
     }  
   } // while cases exist 
 
} // dummyLoad  
 
}
 
 
 
 
public class UVA439 {
 
public static void main(String[] args) throws IOException {
  dummyLoad();
}
 
static void dummyLoad() throws IOException {
  int  N;
  String line;
	BufferedReader br = new BufferedReader( new InputStreamReader(System.in) );
 
  while( true ) {
    // stop when no more input
    if( (line =  br.readLine()) == null ) break;
 
    // process line of input
    StringTokenizer st = new StringTokenizer( line );    
    System.out.printf( "Hello!\n" );
 
    } // while cases exist 
  } // dummyLoad  
}
 
 
 
 
public class UVA459 {
 
public static void main(String[] args) throws IOException {
  dummyLoad();
}
 
static void dummyLoad() throws IOException {
  int  N;
  String line;
  BufferedReader br = new BufferedReader( new InputStreamReader(System.in) );
  StringTokenizer st = new StringTokenizer( br.readLine() );  
 
  N = Integer.valueOf( st.nextToken() );
  br.readLine(); // skip empty
 
  for( int i = 0; i < N; i++ ) { // read cases 
 
    while( true ) {
      line = br.readLine(); if( line == null ) break; // last line of input?
          // be careful, there is no empty line after the last case
      st = new StringTokenizer( line );  
      // --> extract info from the line <-- 
    }
    // --> process case here <--
    System.out.printf( "Hello!\n" );
 
  } // for all cases
} // dummyLoad  
 
}
 
 
 
public class UVA567 {
 
public static void main(String[] args) throws IOException {
  dummyLoad();
}
 
static void dummyLoad() throws IOException {
  int  NQ;
  String line;
 
	BufferedReader br = new BufferedReader( new InputStreamReader(System.in) );
  StringTokenizer st;
 
  line = br.readLine();
  while( true ) {
 
    // graph
    for( int i = 0; i < 19; i++ ){
      st = new StringTokenizer( line ); 
      if( i < 18)  line = br.readLine(); 
      // --> extract info from the tokenizer, edges 
    }
    // no of queries
    st = new StringTokenizer( br.readLine() ); 
    NQ = Integer.valueOf( st.nextToken() );
 
    for( int i = 0; i < NQ; i++ ){
      st = new StringTokenizer( br.readLine() ); 
      // --> extract info from the tokenizer, query 
      // react to the query
      System.out.printf( "Hello!\n" );     
    }
 
    // try to find next case:
    line = br.readLine();
    if( line == null ) break; 
 
    } // while cases exist 
  } // dummyLoad  
}
 
 
 
 
 
public class UVA699 {
 
public static void main(String[] args) throws IOException {
  dummyLoad();
}
 
// input values are randomly scatterd over the input lines
// (but in correct order, of course).
 
// before accesing any value in the  input 
// check that your buffer (String Tokenizer in this case)
// contains something to be read and processed. 
 
// strangely, in this case we HAVE to interpret the input values
// to be able to detect the end of the input/cases correctly
 
static void dummyLoad() throws IOException {
  int  key;
  int [] stack = new int [100000]; 
  int SP = -1; // Stack Pointer;
 
  String line;
	BufferedReader br = new BufferedReader( new InputStreamReader(System.in) );
  StringTokenizer st;
 
  // get first key
  st = new StringTokenizer( br.readLine() ); 
  while( !st.hasMoreTokens() ) st = new StringTokenizer( br.readLine() ); 
  key = Integer.valueOf( st.nextToken() ); 
  stack[++SP] = 1; // first visit
 
 
  while( true ) {
 
    if (SP == -1) { //start reading new tree or break if at the end of input;
      while( !st.hasMoreTokens() ) st = new StringTokenizer( br.readLine() ); 
      key = Integer.valueOf( st.nextToken() ); 
      if( key > 0 ) stack[++SP] = 1; // push new root
      else  break; 
      }
 
    else { // reading inside the tree
      while( !st.hasMoreTokens() ) st = new StringTokenizer( br.readLine() ); 
      key = Integer.valueOf( st.nextToken() ); 
      if( key > 0 ) stack[++SP] = 1;
      else{ // -1 read, move up the tree
        while( SP >= 0 && stack[SP] == 2 ) SP--; // third visit, move up
        if( SP >= 0 ) stack[SP]++; // second visit to a node
        else { // finished tree reading, produce output 
          System.out.printf( "Hello!\n" );         
        }
      }  
    }
 
    } // while cases exist 
  } // dummyLoad  
}
 
 
 
 
 
public class UVA10069 {
 
public static void main(String[] args) throws IOException {
  dummyLoad();
}
 
static void dummyLoad() throws IOException {
  int  N;
 
	BufferedReader br = new BufferedReader( new InputStreamReader(System.in) );
  StringTokenizer st1, st2;
  st1 = new StringTokenizer( br.readLine() );
  N = Integer.valueOf( st1.nextToken() );
 
  for( int i = 0; i < N; i++ ){
    // 2 strings:
    st1 = new StringTokenizer( br.readLine() );
    st2 = new StringTokenizer( br.readLine() );
 
    System.out.printf( "Hello!\n" );
 
  } // while cases exist
} // dummyLoad
}
 
 
 
 
 
 
public class UVA10849 {
 
public static void main(String[] args) throws IOException {
  dummyLoad();
}
 
static void dummyLoad() throws IOException {
  int  N, tests;
 
	BufferedReader br = new BufferedReader( new InputStreamReader(System.in) );
  StringTokenizer st;
  st = new StringTokenizer( br.readLine() );    
  N = Integer.valueOf( st.nextToken() );
 
  br.readLine(); // skip empty line
 
  for( int i = 0; i < N; i++ ){
    st = new StringTokenizer( br.readLine() ); 
    tests = Integer.valueOf( st.nextToken() );
    st = new StringTokenizer( br.readLine() ); // board size
    for( int j = 0; j < tests; j++ ){
      st = new StringTokenizer( br.readLine() ); // query
      // --> process query
    }
    if( i < N-1 )
      br.readLine(); // skip empty line
 
    // print solution
    System.out.printf( "Hello!\n" );      
  } // while cases exist 
 
} // dummyLoad  
 
}
 
 
 
 
public class UVA11195 {
 
public static void main(String[] args) throws IOException {
  dummyLoad();
}
 
static void dummyLoad() throws IOException {
  int  N;
 
	BufferedReader br = new BufferedReader( new InputStreamReader(System.in) );
  StringTokenizer st;
 
  while( true ) {
 
    st = new StringTokenizer( br.readLine() );
    N = Integer.valueOf( st.nextToken() );
    if( N == 0 ) break;
 
    for( int i = 0; i < N; i++ ){
      st = new StringTokenizer( br.readLine() );
      // --> register one row
    }
 
    // process case and output:
    System.out.printf( "Hello!\n" );
 
    } // while cases exist
  } // dummyLoad
}
 
 
 
 
 
public class UVA11396 {
 
public static void main(String[] args) throws IOException {
  dummyLoad();
}
 
static void dummyLoad() throws IOException {
  int  N, a, b;
 
	BufferedReader br = new BufferedReader( new InputStreamReader(System.in) );
  StringTokenizer st;
 
  while( true ) {
 
    st = new StringTokenizer( br.readLine() ); 
    N = Integer.valueOf( st.nextToken() );
    if( N == 0 ) break;
 
    while( true ){ // read edges
      st = new StringTokenizer( br.readLine() );
      a = Integer.valueOf( st.nextToken() );
      b = Integer.valueOf( st.nextToken() );
      if( a == 0 && b == 0 ) break;
    }
 
    // process case and output:
    System.out.printf( "Hello!\n" );
 
    } // while cases exist 
  } // dummyLoad  
}
 
 
 
 
 
public class UVA11504 {
 
public static void main(String[] args) throws IOException {
  dummyLoad();
}
 
static void dummyLoad() throws IOException {
  int  cases,  m, n, a, b;
 
	BufferedReader br = new BufferedReader( new InputStreamReader(System.in) );
  StringTokenizer st =  new StringTokenizer( br.readLine() ); ;
 
  cases = Integer.valueOf( st.nextToken() );
 
 for( int i = 0; i < cases; i++ ) {  
    st = new StringTokenizer( br.readLine() ); 
    n = Integer.valueOf( st.nextToken() );
    m = Integer.valueOf( st.nextToken() );
 
    for( int j = 0; j < m; j++ ){ // read edges
      st = new StringTokenizer( br.readLine() );
      a = Integer.valueOf( st.nextToken() );
      b = Integer.valueOf( st.nextToken() );
    }
    // process case and output:
    System.out.printf( "Hello!\n" );
  }
} // dummyLoad  
 
}

courses/a4b36acm2/2018_zs/input_codes.txt · Last modified: 2018/12/19 22:28 by berezovs