NC54483. Fraud Busters
描述
输入描述
The first line of the input file contains 9 characters of the code as recognized by the scanner. Code that was recognized by the the scanner is represented as a sequence of 9 digits, uppercase English letters, and characters “*” (star). Star represents a digit or a letter that scanner could not recognize.
The second line of the input file contains a single integer number n (1 ≤ n ≤ 1000) — the number of vehicle registration codes from the vehicle registration database.
The following n lines contain the corresponding registration codes, one code per line. Vehicle registration codes are represented as a sequence of 9 digits and uppercase English letters. All codes on these n lines of the input file are different.
输出描述
On the first line of the output file write a single integer k (0 ≤ k ≤ n) — the number of codes from the input file that match the code that was recognized by the scanner. The code from the scanner matches the code from the database if the characters on all the corresponding positions in the codes are equal or the character from the scanner code is “*”.
On the following k lines write the matching codes, one code per line, in the same order as they are given in the input file.
示例1
输入:
A**1MP19* 4 A001MP199 E885EE098 A111MP199 KT7351TTB
输出:
2 A001MP199 A111MP199
C++14(g++5.4) 解法, 执行用时: 4ms, 内存消耗: 476K, 提交时间: 2020-10-05 12:20:11
#include<bits/stdc++.h> using namespace std; string st,s[1005],t[1005]; int n; int main() { cin>>st; cin>>n; int ans=0; for (int i=1;i<=n;i++) { cin>>s[i]; int spj=1; for (int j=0;j<=8;j++) if (st[j]!='*'){ if (st[j]!=s[i][j]) spj=0; } if (spj) t[++ans]=s[i]; } cout<<ans<<endl; for (int i=1;i<=ans;i++) cout<<t[i]<<endl; }
C++11(clang++ 3.9) 解法, 执行用时: 4ms, 内存消耗: 376K, 提交时间: 2020-10-05 12:39:08
#include<bits/stdc++.h> using namespace std; string str,s; vector<string> v; int main(){ int i,j,n; cin>>str; cin>>n; for(i=0;i<n;i++){ cin>>s; for(j=0;j<9;j++){ if(str[j]!='*'&&str[j]!=s[j]){ break; } } if(j>=9)v.push_back(s); } cout<<v.size()<<endl; for(auto x:v){ cout<<x<<endl; } return 0; }
Python3(3.5.2) 解法, 执行用时: 25ms, 内存消耗: 3812K, 提交时间: 2020-10-05 13:36:56
import re pattern = input().replace("*",".") n = int(input()) s = [input() for i in range(n)] ans = [] for i in s: if re.match(pattern,i): ans.append(i) print(len(ans)) for i in ans: print(i)
pypy3(pypy3.6.1) 解法, 执行用时: 145ms, 内存消耗: 27632K, 提交时间: 2020-10-05 12:11:05
import re S=input() S=S.replace("*",".") n=int(input()) L=[] for _ in range(n): s=input() if(re.match(S,s)!=None): L.append(s) print(len(L)) for i in L: print(i)