代理服务器--java/c++

题目如下:

      使用代理服务器能够在一定程度上隐藏客户端信息,从而保护用户在互联网上的隐私。我们知道n个代理服务器的IP地址,现在要用它们去访问m个服务器。这 m 个服务器的 IP 地址和访问顺序也已经给出。系统在同一时刻只能使用一个代理服务器,并要求不能用代理服务器去访问和它 IP地址相同的服务器(不然客户端信息很有可能就会被泄露)。在这样的条件下,找到一种使用代理服务器的方案,使得代理服务器切换的次数尽可能得少。

                               注:重点看注释部分,为程序易错点

输入描述:

每个测试数据包括 n + m + 2 行。
    第 1 行只包含一个整数 n,表示代理服务器的个数。
    第 2行至第n + 1行每行是一个字符串,表示代理服务器的 IP地址。这n个 IP地址两两不相同。
    第 n + 2 行只包含一个整数 m,表示要访问的服务器的个数。
    第 n + 3 行至第 n + m + 2 行每行是一个字符串,表示要访问的服务器的 IP 地址,按照访问的顺序给出。
    每个字符串都是合法的IP地址,形式为“xxx.yyy.zzz.www”,其中任何一部分均是0–255之间的整数。输入数据的任何一行都不包含空格字符。
     其中,1<=n<=1000,1<=m<=5000。

输出描述:

    可能有多组测试数据,对于每组输入数据, 输出数据只有一行,包含一个整数s,表示按照要求访问服务器的过程中切换代理服务器的最少次数。第一次使用的代理服务器不计入切换次数中。若没有符合要求的安排方式,则输出-1。

java实现:

import java.io.*;
import java.util.*;
public class Main{
    public static void main(String []args)throws Exception{
    	String []a=new String[1000];                 
 /*若用ArrayList类型存储,由于solve函数需要使用递归,在ArrayList.subList()会有麻烦。*/
    	String []b=new String[1000];
    	int k=0;
        Scanner sc=new Scanner(System.in);
        String s=sc.nextLine();
        int n1=Integer.valueOf(s);
        int num1=n1;                                   
/*因为下面有--操作,因此需要把原值记录下来,以供递归函数参数使用,否则会导致max一直为-1,因为n1已经为0*/
        while(n1!=0){
            a[k++]=((String)sc.nextLine());
            n1--;
        }
        k=0;
        s=sc.nextLine();
        int n2=Integer.valueOf(s);
        int num2=n2;
        while(n2!=0){
            b[k++]=((String)sc.nextLine());
            n2--;
        }
        System.out.println(solve(a,num1,b,num2)+"");
 }
    static int solve(String[] a,int n1,String [] b,int n2){
        int max=-1;
        int i=0,j=0;
        for(i=0;i<n1;i++){
            for(j=0;j<n2;j++){
                if(b[j].equals(a[i])){
                    if(max<j){
                        max=j;
                    }
                    break;                     /*注意break所在位置*/
                }
            }
            if(j==n2){
                    return 0;
                }
         }
        if(n1==1 && max!=-1){
                return -1;
            }
        return 1+solve(a,n1,Arrays.copyOfRange(b, max, b.length),n2-max);
/*java中数组的子数组不可直接进行加减运算,因此需要用此函数,返回下标为max-b.length-1的元素*/
    }
}

C++实现:

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int solve(string a[],int n1,string b[],int n2){  //注意C++中string首字母小写
    int i,j,max;
    max=-1;
    for(i=0;i<n1;i++){
        for(j=0;j<n2;j++){
            if(!a[i].compare(b[j])){
                //注意此处比较字符串相等的C++方法,若想等返回值为0
                if(max<j)
                    max=j;
                break;
            }
        }
        if(j==n2) return 0;
    }
    if(n1==1 && max!=-1)return -1;
    return 1+solve(a,n1,b+max,n2-max);
}
int main(){
    int n1;
    int n2;
    int i=0;
    cin>>n1;
    string a[n1];            //注意此处变量声明与java的差异性
    for(i;i<n1;i++){
        cin>>a[i];
     }
    cin>>n2;
    string b[n2];
    i=0;
    for(i;i<n2;i++){
        cin>>b[i];
     }
    cout<<solve(a,n1,b,n2);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
基本原理: 代理服务器打开一个端口接收浏览器发来的访问某个站点的请求,从请求的字符串中解析出用户想访问哪个网页,让后通过URL对象建立输入流读取相应的网页内容,最后按照web服务器的工作方式将网页内容发送给用户浏览器 源程序: import java.net.*; import java.io.*; public class MyProxyServer { public static void main(String args[]) { try { ServerSocket ss=new ServerSocket(8080); System.out.println("proxy server OK"); while (true) { Socket s=ss.accept(); process p=new process(s); Thread t=new Thread(p); t.start(); } } catch (Exception e) { System.out.println(e); } } }; class process implements Runnable { Socket s; public process(Socket s1) { s=s1; } public void run() { String content=" "; try { PrintStream out=new PrintStream(s.getOutputStream()); BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream())); String info=in.readLine(); System.out.println("now got "+info); int sp1=info.indexOf(' '); int sp2=info.indexOf(' ',sp1+1); String gotourl=info.substring(sp1,sp2); System.out.println("now connecting "+gotourl); URL con=new URL(gotourl); InputStream gotoin=con.openStream(); int n=gotoin.available(); byte buf[]=new byte[1024]; out.println("HTTP/1.0 200 OK"); out.println("MIME_version:1.0"); out.println("Content_Type:text/html"); out.println("Content_Length:"+n); out.println(" "); while ((n=gotoin.read(buf))>=0) { out.write(buf,0,n); } out.close(); s.close(); } catch (IOException e) { System.out.println("Exception:"+e); } } };

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值