一道有趣的java题

<script type='text/javascript'> google_ad_client = 'pub-8112432724706407'; google_ad_width = 728; google_ad_height = 90; google_ad_format = '728x90_as'; google_ad_type = 'text_image'; google_ad_channel = ''; </script><script type='text/javascript' src='http://pagead2.googlesyndication.com/pagead/show_ads.js'> </script>

这是我在网上碰到的一道题,挺有意思的,拿出来给大家分享。

其实这是这个网站http://www.myjavaserver.com/signup 的一道申请空间的挑战题目,如果你答对了,就可以申请5m的jsp空间,这个网站主要是针对java爱好者的,不妨试试。

后面附上我做的答案。

我的答案没有错,只不过他的网页要求返回的常量是随机产生的,大家可以把答案中的“52AUZZ”替换成网页中要求的值。

 

As the principal engineer of an HTTP web server, you are responsible for implementing the request processing subsystem of the server.
An incoming request for a specific resource, identified by an URI, must be dispatched to the appropriate handler according to the server configuration which maps URIs to request handlers. 'HandlerFactory.getHandler' must be implemented:

public class HandlerFactory
{
  public String getHandler(String[] config, String requestUri)
  {
  }
}

 

The string array 'config' contains URI patterns and handler names. Two consecutive values form a key-value pair comprised of URI pattern and handler. 'requestUri' represents an incoming request, the URI to match against the configured handlers. 'getHandler' must return the correct handler for a given URI as a string value.

An URI pattern never contains wildcards and represents the start of an URI string, a prefix. Matching must be implemented accordingly. The handler with the longest matching URI pattern wins if more than one pattern matches. If no handler can be found, "vX0N2S0" must be returned.

Example input:

String[] config: { "/", "MainServlet", "/nav", "NavigationServlet" }
String requestUri: "/nav/test"

Correct result: "NavigationServlet"

In this example, the configuration contains a mapping of "/" to "MainServlet" and "/nav" to "NavigationServlet". In the case of an incoming URI "/nav/test.nav", "NavigationServlet" is the correct choice because its pattern is longer than that of "MainServlet".

    String returnStr=null;
    boolean hasEqual=false;
    boolean hasStartWith=false;
    int max=0;
    for(int i=0;i<config.length;i=i+2)
    {
      if(config[i].equals(requestUri) )
      {
        if(max<config[i].length())
        {
          max = config[i].length();
          returnStr = config[i + 1];
          hasEqual=true;
        }
      }
      else if(requestUri.startsWith(config[i]))
      {
        if(hasEqual)
        {
          continue;
        }
        else
        {
          hasStartWith=true;
          for(int j=0;j<=config[i].length();j++)
          {
            String s=config[i].substring(0,j);
            if(requestUri.startsWith(s))
            {
              if(max<j)
              {
                max=j;
                returnStr=config[i+1];
              }
            }
          }
        }
      }
      else
      {
        if(hasEqual || hasStartWith)
        {
          continue;
        }
        else
        {
          for(int j=0;j<=config[i].length();j++)
          {
            String s=config[i].substring(0,j);
            if(requestUri.startsWith(s))
            {
              if(max<j)
              {
                max=j;
                returnStr=config[i+1];
              }
            }
          }
        }
      }
    }
    if(!hasEqual &&!hasStartWith && max<2)
      return "52AUZZ";
    return returnStr;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值