2016-10-22 68 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); 
       } 
      } 

`