Web系统通常会频繁地访问数据库,如果每次访问都创建新连接,性能会很差。为了提高性能,架构师决定复用已经创建的连接。当收到请求,并且连接池中没有剩余可用的连接时,系统会创建一个新连接,当请求处理完成时该连接会被放入连接池中,供后续请求使用。
现在提供你处理请求的日志,请你分析一下连接池最多需要创建多少个连接。
输入包含多组数据,每组数据第一行包含一个正整数n(1≤n≤1000),表示请求的数量。
紧接着n行,每行包含一个请求编号id(A、B、C……、Z)和操作(connect或disconnect)。
对应每一组数据,输出连接池最多需要创建多少个连接。
6 A connect A disconnect B connect C connect B disconnect C disconnect
2
#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; }
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); } } }
#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; }
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
#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; }
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); } } }
#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; }
#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; }
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); } } }
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存储最大连接池个数
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); } } }
//其实求解的就是最大连接数 #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; }
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(); } }
#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; } }
#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; }