上一篇:开网店卖什么最赚钱?-- 推荐给店主们一个网站
下一篇:下载Eclipse的链接
招聘测试:用jersey获得twitter网页,获得有最大共有字符串的两条twitts
米娅 2012年2月4日 19:54:48

做了一次测试,感觉很有收获,把代码贴下来共享一下。

题目是使用Jersey获得twitter的某用户的timeline,找出有最大共有字符串的两条twitts。要用maven运行。

我使用的是java eclipse,自带maven插件, 但需要安装maven,下载解压即可。

如果不用maven,使用Jersey需要下载.jar包,在项目的properties窗口找到library这项,添加到项目中。如果用maven,在pom.xml 添加dependency项即可。代码赋在下面。

比较两个字符串时,用到二维数组。把每个字符串的每个字符放到一个char数组里。一个做x轴,一个做y轴,如果x和y相同,交点处的空格填1,其他为0。

比如:

   A B B

B 0 1 1

B 0 1 1

C 0 0 0

值为1的对角线最长的就是最长共有字符串。如何得到最长对角线,就每个为1的都加上它左上角的值。

如下:

   A B B

B 0 1 1

B 0 1 2

C 0 0 0

这是比较两个字符串的共有字符串。

这里是多个字符串,把字符串放在一个一维数组里,取第一条和后面各条比较,把最大字符串的信息放在变量max里。然后取第二条,依次类推作全部比较。

下面代码是java。

用maven的命令行运行,mvn exec:java -Dexec.mainClass="TestApp.GetLCS.App"

 

 



2楼 2012年2月4日 19:55:12 米娅

package TestApp.GetLCS;

import java.net.URL;
import java.net.MalformedURLException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;

/**
 *
 */
public class App
{
    public static void main( String[] args )
    {
     
     // get twitts using jersey
     
     Client client = Client.create();
     
     WebResource webResource = client.resource("http://twitter.com/statuses/user_timeline.xml?id=cnnbrk&count=199");
     
     String webpage_twitts = webResource.get(String.class);
     
        System.out.println( "\nHi, GetLCS will get twitts with Jersey, please be sure this computer can access the Internet...... \n");
       
//        System.out.print(webpage_twitts);
       
       
        // get all twitts and put in Array twitts_all
       
        String rest = webpage_twitts;
       
        String[] twitts_all = new String[300];

        int i = 0;

        while (rest.indexOf("</text>")>=0) {
         
          String twitts[] = getTwittAndRestPart("<text>","</text>", rest);
          twitts_all[i] = twitts[0];
          rest = twitts[1];         
          i = i + 1;
         
        }
        System.out.print("The total number of the twitts:  " + i);
       
       
        // compare every 2 twitts and get the longest common substring
       
        int max = -1;  // the length of the common substring
       
        char[] char_x;
        char[] char_y;
        int[][] c;
       
        int j = 0;
        int k = 0;
       
        int m = 0;
        int n = 0;
       
        int max_a = 0;
        int max_b = 0;
        int max_j = 0;
        int max_k = 0;
       
        String str_max = "";  // the longest common substring

       
        for (m = 0; m< i-2; m++) {
         for (n = m+1; n< i; n++) {
           
               char_x = twitts_all[m].toCharArray();
               char_y = twitts_all[n].toCharArray();
       
               c = new int [char_x.length][char_y.length];
       
               for (j = 0; j < char_x.length; j++) {
               for (k = 0 ; k < char_y.length; k++) {  
              if(char_x[j] == char_y[k]) {
             if (j >0 && k>0) c[j][k]= 1 + c[j-1][k-1];
             else  c[j][k]= 1;
              } else { c[j][k]= 0; }
            
                if (c[j][k] > max) {
                 max = c[j][k];
                    max_a = m;
                    max_b = n;
                    max_j = j;
                    max_k = k;
                    str_max = twitts_all[m].substring(j-max+1, j+1);
                }
              }
              }
         }
        }
       
        System.out.print("\n\nThe length of the common substring: " + max);
        System.out.print("\n\nOne twitt with the longest common substring: \n" + twitts_all[max_a]);
        System.out.print("\n\nThe other twitt with the longest common substring:\n" + twitts_all[max_b]); 
        System.out.print("\n\nThe longest common substring: \n" + str_max);
       
        System.out.print("\n\nIt's all. Thanks. \n" );
 
    }
   
    /* pick the first twitt and put it in twitt_rest[0]
       and put the rest content in twitt_rest[1]  */
    public static String[] getTwittAndRestPart(String search_str_1, String search_str_2, String content) {

        String[] twitt_rest = {"", ""};

        int twitt_1 = content.indexOf(search_str_1);

        content = content.substring(twitt_1);

        int twitt_2 = content.indexOf(search_str_2);

        twitt_rest[0] = content.substring(6, twitt_2);

        twitt_rest[1] = content.substring(twitt_2 + search_str_2.length());

        return twitt_rest;
    }
}

pom.xml
3楼 2012年2月4日 19:57:06 米娅

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>TestApp</groupId>
  <artifactId>GetLCS</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>GetLCS</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-server</artifactId>
    <version>1.11</version>
</dependency>
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-client</artifactId>
    <version>1.11</version>
</dependency>
  </dependencies>
 
  <dependencyManagement>
   <dependencies>
    <dependency>
     <groupId>junit</groupId>
     <artifactId>junit</artifactId>
     <version>3.8.1</version>
    </dependency>
    <dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-server</artifactId>
    <version>1.11</version>
</dependency>
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-client</artifactId>
    <version>1.11</version>
</dependency>
   </dependencies>
  </dependencyManagement>

</project>

第1页 共1页
相关链接
1. 关于测试Drupal核心及Drupal单元测试功能笔记
2. 一道受益终身的测试题,不要错过哟
3. 擅长抽象思维还是具象思维(测试)
4. 测试:你能找到几张脸?(图)
5. 你能看见海豚么?(图)
6. 压力测试(图)
7. 睡相判断性格(图)
8. [讨论]看了什么感想(图)
9. 俄罗斯军队心理测试(图)
10. 测测你有多感性(图)
11. 测测你有多色(图)
12. 心理承受能力测试(图)
13. 测试:你看到了谁?(图)
14. 测试:五根手指注定的爱情
15. 测测你到底漂亮到什么程度
16. 测试你的电脑性别