2016-10-22 75 views
-4

問:有N個字符串。每個字符串的長度不超過20個字符。還有Q查詢。對於每個查詢,都會給出一個字符串,並且您需要查明此字符串以前發生過多少次。執行稀疏陣列

採樣輸入

[主要列表]

ABA

巴巴

ABA

xzxb

[查詢]

ABA

xzxb

AB

樣本輸出

(ABA出現兩次出現在主列表)

(xzxb在主列表中出現一次)

(AB沒有出現在主力名單)

我的代碼

import java.util.*; 

public class Solution { 

public static void main(String[] args) { 
Scanner scan = new Scanner(System.in); 

int N = scan.nextInt(); // CONTAINS N number of String 
String word[] = new String[N]; //Sets N as size of an array 


for(int i = 0; i < N; i++){ 
    word[i] = scan.nextLine();//Stores a word in every index of an array 

} 

scan.nextLine(); //Flush index??(need help?!) 



int Q = scan.nextInt(); // Stores number of query 
    String searchWord[] = new String[Q];//integer for size of query array 
    for(int i = 0; i <Q; i++){ 
     searchWord[i] = scan.nextLine(); // stores query array for comparison 
    } 


    int counter = 0; // initializing counter 

    for(int i=0; i <Q; i++){//Take a query word and check if it exists in word[] 
     for(int j =0; j <N; j++){//searches for the query word in main List 
      if(word[j] == searchWord[i]){// if it exists counter value adds +1 
       counter++; 
      } 
     } 
     System.out.println(counter); //print counter 
     counter = 0; // reset counter 

    } 
    } 
} 

首先代碼確實不是工作,雖然邏輯看起來是正確的(我猜的)。也有人給我解釋一下爲什麼我們需要消耗換行符左在做

input.nextLine();

源:Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods

問題https://www.hackerrank.com/challenges/sparse-arrays

^^?我如何使用它在我的問題。謝謝! :)

回答

0

的nextLine()是您的來電nextInt()後的數字......包括換行後消耗任何字符所需。

(這是無關的沖洗指標。什麼??指數)

所以......它看起來就像你在錯誤的地方有它。

0

工作代碼:

BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
    System.out.println("Enter number of main list:"); 
    try { 
     short mainListCount = Short.valueOf(br.readLine()); 
     // if count is valid then reading main list 
     if (mainListCount > 0) { 
      List<String> mainList = new ArrayList<>(); 
      while (mainListCount > 0) { 
       mainList.add(br.readLine()); 
       mainListCount--; 
      } 

      // getting query count 
      System.out.println("\nEnter number of Query:"); 
      short queryCount = Short.valueOf(br.readLine()); 
      if (queryCount > 0) { 
       String queryStr; 
       String result = ""; 
       while (queryCount > 0) { 
        queryStr = br.readLine(); 
        result += " Occurance of " + queryStr + ": " + Collections.frequency(mainList, queryStr) + "\n"; 
        queryCount--; 
       } 
       System.out.println("\n" + result); 
      } 
      System.out.println("\n\n**program ends**"); 
     } else { 
      System.out.println("Invalid count"); 
     } 
    } catch (NumberFormatException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

關於掃描儀: 掃描儀打破了它的投入使用定界符模式,默認情況下與空白匹配。 所以當你做scan.next()scan.nextInt()它讀取,直到它遇到空格。例如,如果輸入4 4(兩者之間的空格),則只會使用4作爲值,而不會讀取行結尾「\ n」。 scan.nextLine()直到行尾。這就是爲什麼scan.nextLine()scan.next()之後被跳過的原因,因爲它由於「Enter」而顯示控制檯中已存在的「\ n」(行尾)。

0

C#實現,但可以很容易地轉換爲java` '

int totalInputs = Convert.ToInt16(Console.ReadLine()); 
      Dictionary<string, int> dict = new Dictionary<string, int>(); 
      for(i = 0; i< totalInputs;i++) 
      { 
       string input = Console.ReadLine(); 
       if(dict.ContainsKey(input)) 
       { 
        dict[input]++; 
       } 
       else 
       { 
        dict.Add(input, 1); 
       } 
      } 
      int queries = Convert.ToInt16(Console.ReadLine()); 
      string[] queryString = new string[queries]; 
      for(i=0;i<queries;i++) 
      { 
       queryString[i] = Console.ReadLine(); 
      } 
      foreach(string str in queryString) 
      { 
       if(dict.ContainsKey(str)) 
       { 
        Console.WriteLine(dict[str]); 
       } 
       else 
       { 
        Console.WriteLine(0); 
       } 
      } 

`