首页 > 试题广场 >

数据库连接池

[编程题]数据库连接池
  • 热度指数:1786 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
Web系统通常会频繁地访问数据库,如果每次访问都创建新连接,性能会很差。为了提高性能,架构师决定复用已经创建的连接。当收到请求,并且连接池中没有剩余可用的连接时,系统会创建一个新连接,当请求处理完成时该连接会被放入连接池中,供后续请求使用。

现在提供你处理请求的日志,请你分析一下连接池最多需要创建多少个连接。

输入描述:
输入包含多组数据,每组数据第一行包含一个正整数n(1≤n≤1000),表示请求的数量。

紧接着n行,每行包含一个请求编号id(A、B、C……、Z)和操作(connect或disconnect)。


输出描述:
对应每一组数据,输出连接池最多需要创建多少个连接。
示例1

输入

6
A connect
A disconnect
B connect
C connect
B disconnect
C disconnect

输出

2
用set做的,这个方法麻烦了一点。。。想的太多了。。。
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
#include <set>
using namespace std;

int main()
{
	int n;
	while (cin >> n)
	{
		set<string> pool;
		string id, con;
		int maxSize = 0;
		for (int i = 0; i < n; ++i)
		{
			cin >> id >> con;
			if (con == "connect") pool.insert(id);
			else if (con == "disconnect") pool.erase(id);
			int size = pool.size();
			maxSize = max(maxSize, size);
		}
		cout << maxSize << endl;
	}
	return 0;
}

发表于 2015-12-16 12:20:25 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = sc.nextInt();
            int size = 0;
            int max = 0;
            for(int i = 0;i < n;i++){
                String id = sc.next();
                String s = sc.next();
                
                if(s.equals("connect")){
                    size++;
                    max = Math.max(max,size);
                }else if(s.equals("disconnect")){
                    size--;
                }
            }
            System.out.println(max);
        }
    }
}

编辑于 2022-05-25 22:02:19 回复(0)
#include <iostream> #include <stdlib.h> #include <string> using namespace std;
int main() {  int n;  char id;  string str;
 while (cin >> n) {         int sum = 0;      int max = 0;   for (int i = 0; i < n; ++i) {    cin >> id >> str;    if (str == "connect") {     sum++;     if (sum > max) {      max = sum;     }    }    else if (str == "disconnect") {     --sum;    }   }   cout << max << endl;  }
 system("pause");  return 0; }

发表于 2019-07-28 11:32:57 回复(0)
//维护过程中最大的链接数(不包括释放的)
import java.util.*;
import java.io.*;
public class Main{
    public static void main(String[] args)throws IOException{
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String strn=null;
        while((strn=br.readLine())!=null){
        int n=Integer.valueOf(strn);
        int consum=0;
        int ret=0;
        for(int i=0;i<n;i++){
            String[] str=br.readLine().split(" ");
            if(str[1].equals("connect"))
                consum++;
            else
                consum--;
            ret=Math.max(ret,consum);
        }
        System.out.println(ret);
        }
    }
}
 
发表于 2019-05-12 15:16:39 回复(0)

python解法

使用一个列表来做,如果某个记录是connect,就加到列表中去,如果是disconnect,就把列表中对应的值删除掉。在加进去的过程中,不断判断列表的最大长度,最终返回这个最大长度即可。

while True:
    try:
        stack, maxNum = [], 0
        for _ in range(int(input())):
            left, right = input().split()
            if right == "connect":
                stack.append(left)
                maxNum = max(maxNum, len(stack))
            else:
                stack.remove(left)
        print(maxNum)
    except:
        break
发表于 2017-11-16 10:27:43 回复(0)
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<functional>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <exception>
#include <iomanip>
#include <memory>
#include <sstream>

using namespace std;

bool check(int x, int y, int m, int n, vector<vector<char>>& room)
{
	return x >= 0 && x < m && y >= 0 && y < n && room[x][y] == '.';
}

int main(int argc, char** argv)
{
	//freopen("in.txt", "r", stdin);
	int n;
	while (cin >> n)
	{
		unordered_set<string> connect;
		int count = 0;
		string id, op;
		while (n-- > 0)
		{
			cin >> id >> op;
			if (connect.find(id) != connect.end())
			{
				if (op == "disconnect")
					connect.erase(id);
			}
			else
			{
				if (op == "connect")
				{
					if (count == connect.size()) ++count;
					connect.emplace(id);
				}
			}
		}

		cout << count << endl;
	}

	return 0;
}

发表于 2017-07-10 22:25:44 回复(0)
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class _t354 {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		while(in.hasNext()){
			int n=in.nextInt(),rs=0;
			Queue<String> qe=new LinkedList<String>(); 
			for (int i = 0; i < n; i++) {
				String id=in.next();
				String status=in.next();
				qe.add(status);
			}
			
			int flag=0;
			for (int i = 0; i < n; i++) {
				if(qe.poll().equals("connect")){
					if(flag==0){
						rs++;
					}
					flag++;
				}
				else{
					flag--;
				}
			}
			System.out.println(rs);
		}
	}
}
前面这个ID是不是没什么用的?还是我题目理解的还不对,求特殊案例分析,一针见血~

发表于 2017-05-10 14:22:22 回复(1)
try:
    while 1:
        maxVal, crnVal = 0, 0
        for i in xrange(input()):
            s = raw_input().split()
            if s[1] == "disconnect":
                crnVal -= 1
            else:
                crnVal += 1
                if crnVal > maxVal:
                    maxVal = crnVal
        print maxVal
except:
    pass

发表于 2017-05-05 11:12:55 回复(0)
#include<stdio.h>


int main()
{
    char id[2], action[12];
    int n, pool, max;
    
    while(~scanf("%d", &n)){
        pool = max = 0;
        while(n--){
            scanf("%s%s", id, action);
            if(action[0] == 'c' && ++pool > max)
                 max = pool;
            if(action[0] == 'd')
                 --pool;
        }
        printf("%d\n", max);
    }
    return 0;
}

发表于 2015-09-16 10:19:50 回复(2)
#include<iostream>
#include<string>
using namespace std;
int main()
{
    int n;
    while(cin>>n)
    {
        int count=0,max_count=0;
        string id,method;
        while(n--)
        {
            cin>>id>>method;//其实用不上id...
            if(method=="connect")     ++count;
            if(method=="disconnect")  --count;
            if(count>max_count)        max_count=count;
        }
        cout<<max_count<<endl;
    }
    return 0;
}

编辑于 2020-03-08 20:56:03 回复(0)
import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = sc.nextInt();
            Set<String> s = new HashSet<>();
            int max_count = 0;
            while(n != 0){
                String id = sc.next();
                String op = sc.next();
                if(op.equals("connect")){
                    s.add(id);
                }else{
                    s.remove(id);
                }
                max_count = Math.max(max_count,s.size());
                n--;
            }
            System.out.println(max_count);
        }
    }
}

发表于 2023-03-07 21:00:51 回复(0)
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()) {
            String  str=scanner.nextLine();
            int n=Integer.parseInt(str);
            scanner.next();
            String[] elem = new String[n];
            for (int i = 0; i < n; i++) {
                elem[i] = scanner.nextLine();
            }
            Queue<String> queue = new LinkedList<>();
            int size = 0;
            for (int i = 0; i < elem.length; i++) {
                String[] ret = elem[i].split(" ");
                if (ret[1].equals("connect")) {
                    queue.add(ret[0]);
                    size = Math.max(size, queue.size());
                } else
                    queue.poll();
            }
            System.out.println(size);
        }
    }
}

发表于 2022-09-20 11:48:02 回复(0)
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNextLine()){
            String strNum = scanner.nextLine();
            int num = Integer.parseInt(strNum);
            int count = 0;
            int maxCount = 0;
            while(num-- != 0){
                String str = scanner.nextLine();
                if(!str.contains("disconnect")){
                    count++;
                }else{//那就是connect
                    maxCount = count > maxCount ? count : maxCount;
                    count--;
                }
            }
                System.out.println(maxCount);
            }
    }
}

只要出现discnnonet那就将当前count次数和maxCount次数进行比较,maxCount存储最大连接池个数
编辑于 2022-06-14 18:21:32 回复(0)
#include<iostream>
#include<stack>
using namespace std;

int main()
{
    int n;
    while(cin>>n)
    {
        stack<string>st;
        string id,opt;
        size_t  ret=0;
        while(n--)
        {
            cin>>id>> opt;
            if(opt=="connect")
            {
                st.push(id);
            }
            else
            {
                st.pop();
            }
            ret=std::max(ret,st.size());
        }
        cout<<ret<<endl;
    }
}
发表于 2021-11-22 11:36:05 回复(0)
您写您ma的题目呢,我屮艸芔茻
发表于 2021-07-26 23:42:34 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = Integer.parseInt(sc.nextLine());
            String[] str = new String[n];
            Stack<String> stack = new Stack<>();
            int max = 0;
            for(int i = 0; i < n; i++){
                str[i] = sc.nextLine();
                if(!str[i].contains("disconnect")){
                    stack.push(str[i]);
                    max = Math.max(max, stack.size());
                }
                else{
                    stack.pop();
                }
            }
            System.out.println(max);
        }
    }
}

发表于 2021-07-26 12:51:47 回复(0)
//其实求解的就是最大连接数
#include<iostream>
#include<string>
using namespace std;
int main()
{
    int n;
    while(cin>>n)
    {
        int count=0,max_count=0;
        string id,status;
        while(n--)
        {
            cin>>id>>status;//这里id没用到,但是还必须按指定要求输入
            if(status == "connect") ++count;
            if(status == "disconnect") --count;
            if(count>max_count) max_count=count;
        }
        cout<<max_count<<endl;
    }
    return 0;
}

发表于 2021-06-08 19:08:52 回复(0)
import java.util.*;

public class Main {
    
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        
        while (in.hasNextInt()) {
            int n = Integer.parseInt(in.next());
            int curLink = 0, maxLink = 0;
            Set<String> linkPool = new HashSet<>();
            for (int i = 0; i < n; ++i) {
                String name = in.next(), state = in.next();
                if (!linkPool.contains(name) && "connect".equals(state)) {
                    linkPool.add(name);
                    ++curLink;
                    maxLink = Math.max(curLink, maxLink);
                    continue;
                }
                
                if (linkPool.contains(name) && "disconnect".equals(state)) {
                    --curLink;
                    linkPool.remove(name);
                }
            }
            
            System.out.println(maxLink);
        }
        
        in.close();
    }
}

发表于 2021-05-31 16:15:16 回复(0)
一开始看题目高大上, 还准备好好搞搞。
仔细一看题目, 发现并不是很难。 题目还是放水了
#include<iostream>
(720)#include<vector>
#include<string>

using namespace std;

int main(){
    
    int n = 0;
    while(cin >> n){
        
        vector<string> vec;
        vec.resize(n);
        
        getchar();
          
        int sum = 0;
        int count = 0;
        
        //把数据请求读进来
        for(int i = 0; i < n; ++i){
            getline(cin, vec[i]);
            
            if(vec[i].find("disconnect", 1) != string::npos){
                //断开请求
                count--;
            }
            else{
                //连接请求
                count++;
                if(count > sum){
                    sum = count;
                }
            }
        }
        
      cout << sum << endl;
    }
}


发表于 2020-05-14 15:29:00 回复(0)
#include<iostream>
#include<string>
using namespace std;

int main()
{
    int n;
    while(cin>>n)
    {
        int max = 0;
        string con,oper;
        char c;
        while(n--)
        {
            cin>>c>>oper;
            if(oper[0]=='c')
                con.push_back(c);
            else
                con.erase(con.find(c),1);
            max = max<con.size() ? con.size() : max;
        }
        cout<<max<<endl;
    }
    return 0;
}

编辑于 2020-03-10 10:19:21 回复(0)