提交统计提问时间限制: 1000ms内存限制: 65535kB
描述
  一般的文本编辑器都有查找单词的功能,该功能可以快速定位特定单词在文章中的位置,有的还能统计出特定单词在文章中出现的次数。  
现在,请你编程实现这一功能,具体要求是:给定一个单词,请你输出它在给定的文章中出现的次数和第一次出现的位置。注意:匹配单词时,不区分大小写,但要求完全匹配,即给定单词必须与文章中的某一独立单词在不区分大小写的情况下完全相同 (参见样例 1) ,如果给定单词仅是文章中某一单词的一部分则不算匹配(参见样例 2) 。输入
第 1 行为一个字符串,其中只含字母,表示给定单词;  
第 2 行为一个字符串,其中只可能包含字母和空格,表示给定的文章。
输出
只有一行, 如果在文章中找到给定单词则输出两个整数, 两个整数之间用一个空格隔开,分别是单词在文章中出现的次数和第一次出现的位置(即在文章中第一次出现时,单词首字母在文章中的位置,位置从 0 开始) ;如果单词在文章中没有出现,则直接输出一个整数-1。
样例输入
样例 #1:
To  
to be or not to be is a question  样例 #2:
to  
Did the Ottoman Empire lose its power at that time样例输出
样例 #1:
2 0样例 #2:
-1提示
【输入输出样例 1 说明】  
输出结果表示给定的单词 To 在文章中出现两次,第一次出现的位置为 0。【输入输出样例 2 说明】  
表示给定的单词 to 在文章中没有出现,输出整数-1。  【数据范围】  
1 ≤单词长度≤10。  
1 ≤文章长度≤1,000,000。
代码如下::
import java.io.*;
import java.util.*;
 class Main
 {
    public static void main(String[] args)  throws Exception{
        int count = 0;
        int j=0;
        int k=0;
        Scanner sc = new Scanner(System.in);
        String s1 = sc.nextLine().toLowerCase();
        String s2 = sc.nextLine().toLowerCase();
        char ss=s1.charAt(0);
        String[] result = s2.split("\\s+");
        int[] a = new int[s2.length()];
        for (int i = 0; i < result.length; i++) {
            if (s1.equals(result[i])) {
                count++;
            }
        }
        int d= s2.indexOf(s1);
        if (count == 0) {
            System.out.println(Integer.parseInt("-1"));
        } else {
            System.out.println(count + " " + d);
        }
    }
}
提交统计提问时间限制: 1000ms内存限制: 65535kB
描述
  一般的文本编辑器都有查找单词的功能,该功能可以快速定位特定单词在文章中的位置,有的还能统计出特定单词在文章中出现的次数。  
现在,请你编程实现这一功能,具体要求是:给定一个单词,请你输出它在给定的文章中出现的次数和第一次出现的位置。注意:匹配单词时,不区分大小写,但要求完全匹配,即给定单词必须与文章中的某一独立单词在不区分大小写的情况下完全相同 (参见样例 1) ,如果给定单词仅是文章中某一单词的一部分则不算匹配(参见样例 2) 。输入
第 1 行为一个字符串,其中只含字母,表示给定单词;  
第 2 行为一个字符串,其中只可能包含字母和空格,表示给定的文章。
输出
只有一行, 如果在文章中找到给定单词则输出两个整数, 两个整数之间用一个空格隔开,分别是单词在文章中出现的次数和第一次出现的位置(即在文章中第一次出现时,单词首字母在文章中的位置,位置从 0 开始) ;如果单词在文章中没有出现,则直接输出一个整数-1。
样例输入
样例 #1:
To  
to be or not to be is a question  样例 #2:
to  
Did the Ottoman Empire lose its power at that time样例输出
样例 #1:
2 0样例 #2:
-1提示
【输入输出样例 1 说明】  
输出结果表示给定的单词 To 在文章中出现两次,第一次出现的位置为 0。【输入输出样例 2 说明】  
表示给定的单词 to 在文章中没有出现,输出整数-1。  【数据范围】  
1 ≤单词长度≤10。  
1 ≤文章长度≤1,000,000。
代码如下::
Java code
import java.io.*;
import java.util.*;
 public class Main
 {
    public static void main(String[] args)  throws Exception{
        int count = 0;
        int j=0;
        int k=0;
        Scanner sc = new Scanner(System.in);
        String s1 = sc.nextLine().toLowerCase();
        String s2 = sc.nextLine().toLowerCase();
        char ss=s1.charAt(0);
        String[] result = s2.split("\\s+");
        int[] a = new int[s2.length()];
        for (int i = 0; i < result.length; i++) {
            if (s1.equals(result[i])) {
                count++;
            }
        }
        int d= s2.indexOf(s1);
        if (count == 0) {
            System.out.println(Integer.parseInt("-1"));
        } else {
            System.out.println(count + " " + d);
        }
    }
}这个怎么是Wrong answer?????求救 

解决方案 »

  1.   

    什么wrong correct的,能搞出来就可以了。效率不效率的其实也不是很重要。
    实现了还说wrong answer,日。
      

  2.   

    一般OJ上的测试数据都不止一组,所以你要读到EOF为止。import java.util.Scanner;class Main {
    public static void main(String[] args) throws Exception {
    int count = 0;
    Scanner sc = new Scanner(System.in);
    // eof
    while (sc.hasNext()) {
    count = 0;
    String s1 = sc.nextLine().toLowerCase();
    String s2 = sc.nextLine().toLowerCase();
    String[] result = s2.split("\\s+");
    for (int i = 0; i < result.length; i++) {
    if (s1.equals(result[i])) {
    count++;
    }
    }
    int d = s2.indexOf(s1);
    if (count == 0) {
    System.out.println(Integer.parseInt("-1"));
    } else {
    System.out.println(count + " " + d);
    }
    }
    }
    }
      

  3.   

    悲剧,题目都看不懂:
    单词:aa
    文章:aaaaaa
    这样算出现3次,还是5次?
      

  4.   

    二楼改的还是不对?Wrong answer。我急啊
      

  5.   

    s2.split("\\s+");
    这个我没用过这种形式,不知道结果是什么,能保证分割的正确?
    建议在for循环中先输出分割后字符串的内容和长度。先确定问题在哪。
      

  6.   


    import java.util.Scanner;
    public class Main{
    public static void main(String args[]){
    Scanner scan  = new Scanner(System.in) ;
    int count = 0 ;
    int location = -1 ;
    System.out.print("请输入要查找的单词:") ;
    String str1 = scan.nextLine() ;
    System.out.print("请输入文章:") ;
    String str2 = scan.nextLine() ;
    String s[] = str2.split("\\s+") ;
    for(int i=0;i<s.length;i++){
    if(str1.equalsIgnoreCase(s[i])){
    count++ ;
    location = i ;
    }
    }
    System.out.println("count:" + count) ;
    System.out.println("location:" + location) ;
    }
    }
      

  7.   

    没注意要求:
    import java.util.Scanner;
    public class Main{
    public static void main(String args[]){
    Scanner scan  = new Scanner(System.in) ;
    int count = 0 ;
    int location = -1 ;
    int index = 0 ;
    // System.out.print("请输入要查找的单词:") ;
    String str1 = scan.nextLine() ;
    // System.out.print("请输入文章:") ;
    String str2 = scan.nextLine() ;
    String s[] = str2.split("\\s+") ;
    for(int i=0;i<s.length;i++){
    if(str1.equalsIgnoreCase(s[i])){
    count++ ;
    if(count==1){
    location  = i ;
    }
    }
    }
    System.out.println(count + " " + location) ;
    // System.out.println("count:" + count) ;
    // System.out.println("location:" + location) ;
    }
    }