CCF-URL映射-(正则匹配)-20180303

果然正则表达式是一个强大的工具

更短的代码....hhh

版本1: 正则表达式..so easy~~

 #include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <regex>
using namespace std;
const int N = ;
const string s_int = "([0-9]+)";
const string s_str = "([0-9A-Za-z-_\.]+)";
const string s_path = "([0-9A-Za-z-_\./]+)";
string rule[N], na[N];
vector <int> p[N]; // 表示匹配项类型 1-int;2-str;3-path
int n, m;
smatch sm;
string _deal(string str, int k) {// 将"int"->s_int'... 变成正则表达式的情况
string ans;
for (int i = ; i < str.size(); i++) {
if (str[i] == '<') {
string tmp; i++;
while (str[i] != '>')
tmp.push_back(str[i++]);
if (tmp == "int") { ans += s_int; p[k].push_back(); }
else if (tmp == "str") { ans += s_str; p[k].push_back(); }
else { ans += s_path; p[k].push_back(); }
}
else ans.push_back(str[i]);
}
return ans;
}
int main()
{
cin >> n >> m;
for (int i = ; i <= n; i++) {
string s1;
cin >> s1 >> na[i];
rule[i] = _deal(s1, i);
}
for (int i = ; i <= m; i++) {
string str; cin >> str;
string ans = ""; int k = ;
for (; k <= n; k++) {
regex re(rule[k]);
if (regex_match(str,sm,re)) {
ans = na[k];
break;
}
}
cout << ans;
if (ans != "")
for (int j = ; j < p[k].size(); j++) {
if (p[k][j] == ) cout << " " <<stoi(sm[j+]);
else cout << " " << sm[j+];
}
cout << "\n";
}
//system("pause");
return ;

版本2: split() 用/来分割字符串  [以前写的 好复杂啊

 #include <bits/stdc++.h>
using namespace std;
vector <string> name();
vector < vector <string> > rule();
vector <string> ans;
bool wei[];
bool isdight (char x) {
return x>=''&&x<='';
}
bool ischar (char x) {
if (isdight(x) )
return true;
if ( (x>='A' && x<='Z') || (x>='a' && x<='z') )
return true;
if ( x=='/' || x=='-' || x=='_' || x=='.')
return true;
return false;
}
vector <string> split (const string& str,const char flag=' ') {
istringstream iss(str);
vector <string> sv;
string tmp;
while (getline(iss,tmp,flag))
sv.push_back(tmp);
return sv;
}
bool isok (string str) {
if (str[]!='/') return ;
for (int i=;i<str.size();i++) {
if (!ischar(str[i]))
return ;
}
return ; }
string dtoc (int x) {
string str;
if (x==) return "";
while (x) {
str+=x%+'';
x/=;
}
reverse(str.begin(),str.end());
return str;
}
bool ismatch (int k, string str) {
bool flag=;
ans.clear();
if (str[str.size()-]=='/') flag=;
vector <string> sv=split (str,'/'); for (int i=;i<rule[k].size();i++) {
if (i>=sv.size()) return ;
int len=sv[i].size();
if (rule[k][i]=="<int>") {
int sum=;
for (int j=;j<len;j++) {
if (!isdight(sv[i][j])) return ;
sum=sum*+sv[i][j]-'';
}
ans.push_back(dtoc(sum));
} else if (rule[k][i]=="<str>")
ans.push_back(sv[i]); else if (rule[k][i]=="<path>") {
string tmp;
for (int k=i;k<sv.size();k++) {
tmp+=sv[k];
if (k==sv.size()-&&!flag) continue;
tmp+="/";
}
ans.push_back(tmp);
return ;
} else {
if (sv[i]!=rule[k][i]) return ;
} }
if (rule[k].size()==sv.size()&&(flag==wei[k])) return ; // 真的是傻逼
else return ;
}
int n,m;
int main ()
{
cin>>n>>m; getchar();
string s1,s2;
for (int i=;i<=n;i++) {
cin>>s1>>s2; getchar();
name[i]=s2;
if (s1[s1.size()-]=='/') wei[i]=;// 没有考虑的地方
rule[i]=split(s1,'/');
}
for (int i=;i<=m;i++) {
getline (cin,s1); int x=;
if (isok(s1)) {
for (int j=;j<=n;j++)
if (ismatch(j,s1)) {
x=j;
break;
}
}
if (x==) cout<<""<<endl;
else {
cout<<name[x];
for (int j=;j<ans.size();j++)
cout<<" "<<ans[j];
cout<<endl;
}
}
return ;
}
上一篇:OGG for DB2 i 12.2发布


下一篇:如何获取google可以访问的IP地址