当前位置:网站首页>201803-3 Full Score solution of CCF URL mapping
201803-3 Full Score solution of CCF URL mapping
2022-07-29 09:01:00 【A cute little monkey】
Problem description
URL Map the original question link
Their thinking
Regular paths and names are separated by spaces , You can use the cin Read and store separately
May adopt Structure storage Every rule
then url Each entry is read and parsed matching , If the match is successful , Then output the answer , otherwise 404
Implementation skills
Matching implementation , Double pointers can be used , With / For division , Take out the string
Judge the type of string str/int/path/ Constant
Store answers in vector< string > in
During the matching process , If there is a mismatch, clear vector, return
therefore The result size is zero , The match fails
The initialization result size is 1
therefore The result size is 1, Then the match is successful , But no reference
therefore The result size is zero , Then the match is successful , And have reference
Code implementation
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
struct Url
{
string path;
string name;
}url[110];
// Judge whether the string is a string of numbers , And return this string of numbers
string check_num(string num)
{
string res;
for (int i = 0; i < num.size(); i ++)
{
if (num[i] >= '0' && num[i] <= '9') continue;
else
{
res.clear();
return res;
}
}
int k = 0;
while (k < num.size() - 1 && num[k] == '0') k ++; // Remove the prefix zero
res = num.substr(k);
return res;
}
// Judge url Whether it matches the rule , And return the parameter value
vector <string> check(string & path, string & query)
{
//res.size() == 0 Indicates a mismatch
//res.size() == 1 Represents a match , But no reference
//res.size() > 1 Represents a match , And have reference
vector <string> res(1); // Apply for an array of spaces
int i, j;
for (i = 1, j = 1; i < path.size() && j < query.size(); )
{
// Need double pointer instead i, j Go back to , Because the back path We need “ to flash back ”
int u = i + 1, v = j + 1;
while (u < path.size() && path[u] != '/') u ++;
while (v < query.size() && query[v] != '/') v ++;
string type = path.substr(i, u - i);
string value = query.substr(j, v - j);
if (type == "<str>")
{
res.push_back(value);
i = u + 1;
j = v + 1;
}
else if (type == "<int>")
{
auto num = check_num(value);
if (num.size())
{
res.push_back(num);
i = u + 1;
j = v + 1;
}
else
{
res.clear();
return res;
}
}
else if (type == "<path>")
{
res.push_back(query.substr(j));
return res;
}
else if (type != value)
{
res.clear();
return res;
}
else
{
i = u + 1;
j = v + 1;
}
}
// This sentence is cleverly written
// Two conditions are judged
// First rule and url The end must be equal to / Or not equal to /
// Secondly, rules and url Should go to the end at the same time , Excluding partial matching
if (i - path.size() != j - query.size())
{
res.clear();
return res;
}
return res;
}
void work(string & str, int n) // Adding references can reduce replication , Increase speed
{
for (int i = 0; i < n; i ++)
{
auto res = check(url[i].path, str);
// The match is successful , Then output parameters and return
if (res.size())
{
cout << url[i].name;
for (int j = 1; j < res.size(); j ++) //j from 1 Start ,res[0] It is a sign to judge whether the match is successful
{
cout << " " << res[j];
}
cout << endl;
return ;
}
}
// At this time, the description does not match successfully
puts("404");
}
int main()
{
int n, m;
cin >> n >> m;
// Because separated by spaces , So you can use it directly cin Read , Separate storage
for (int i = 0; i < n; i ++) cin >> url[i].path >> url[i].name;
string str;
for (int i = 0; i < m; i ++)
{
// Read and parse each query
cin >> str;
work(str, n);
}
return 0;
}
边栏推荐
- 2022 question bank and answers of operation certificate examination for main principals of hazardous chemical business units
- (视频+图文)机器学习入门系列-第3章 逻辑回归
- Arfoundation starts from scratch 5-ar image tracking
- 【Unity入门计划】C#与Unity-了解类和对象
- Sublime text create page
- [LOJ 6485] LJJ binomial theorem (unit root inversion) (template)
- 6.3 references
- Jetpack Glance? The spring of widgets is coming
- Simple unit testing idea
- Memory leaks and common solutions
猜你喜欢

2022年山东省安全员C证上岗证题库及答案

Sublime text create page

Sword finger offer 27. image of binary tree

QT version of Snake game project

Leetcode: interview question 08.14. Boolean operation

数学建模——微分方程
网络原理笔记(五层网络)

【Unity入门计划】C#与Unity-了解类和对象

Tesseract图文识别--简单

Safety is no longer the only selling point. Test drive "slash youth" Volvo C40
随机推荐
Collation of ml.net related resources
GBase 8s数据库有哪些备份恢复方式
Sword finger offer 32 - ii Print binary tree II from top to bottom
2022 R2 mobile pressure vessel filling test question simulation test platform operation
01背包关于从二维优化到一维
Intel将逐步结束Optane存储业务 未来不再开发新产品
How does xjson implement four operations?
Travel notes in 2022 (ongoing)
2022.7.9 quick view of papers
Leetcode deduction topic summary (topic No.: 53, 3, 141, interview question 022, the entry node of the link in the sword finger offer chain, 20, 19, Niuke NC1, 103, 1143, Niuke 127)
The biggest upgrade of Bluetooth over the years: Bluetooth Le audio is about to appear in all kinds of digital products
Multiple knapsack, simplicity and binary optimization
Can the access database be accessed remotely
Mathematical modeling - Differential Equations
One article tells you the salary after passing the PMP Exam
RESTful 风格详解
Compile and install Apache for rocky Foundation
MySQL 错误总结
MFC integration QT verification and problem handling
2022年R2移动式压力容器充装考题模拟考试平台操作