题目
题目描述
The number of cars in Default City that travel to the city center daily vastly exceeds the number of available parking spots. The City Council had decided to introduce parking fees to combat the problem of overspill parking on the city streets. Parking fees are enforced using an automated vehicle registration plate scanners that take a picture of the vehicle registration plate, recognize the sequence of digits and letters in the code on the plate, and check the code against a vehicle registration database to ensure that parking fees are dutifully paid or to automatically issue a fine to the vehicle owner otherwise.
As soon as parking fees were introduced, a parking fee fraud had appeared. Some vehicle owners had started to close one or several digits or letters on their vehicle registration plate with pieces of paper while they park, thus making it impossible for the current version of the automated scanner to recognize their vehicle's registration code and to issue them a fine.
The Default City Council had instituted the Fraud Busters Initiative (FBI) to design a solution to prevent this kind of fraud. The overall approach that FBI had selected is to expand the number of vehicle features that scanners recognize (including features like vehicle type and color), as well as excluding from the list any vehicles that are detected to be elsewhere at this time. This information should help to identify the correct vehicle by narrowing down the search in the vehicle registration database.
You are working for FBI. Your colleagues had already written all the complex pieces of the recognition software that analyses various vehicle features and provides you with a list of registration codes that might potentially belong to a scanned car. Your task it to take this list and a recognized code from the license plate (which may be partially unrecognized) and find all the registration codes that match.
输入格式
The first line of the input file contains 99 characters of the code as recognized by the scanner. Code that was recognized by the the scanner is represented as a sequence of 99 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 �(1≤�≤1000)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 99 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 �(0≤�≤�)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.
题意翻译
给定一个含未知字符的长度为9字符串,未知字符用'*'表示(不含引号)。下面给出n个长度为9字符串,如果下面输入的字符串与一开始输入的字符串除未知部分的其他部分完全相同,则该字符串符合要求。输出符合要求的字符串的数量和每个符合要求的字符串。
举例:假设一开始输入的字符串为A58**52*1,则字符串A58ZS52T1符合要求,而字符串A589992G1不符合要求
输入输出样例
输入 #1复制
A**1MP19* 4 A001MP199 E885EE098 A111MP199 KT7351TTB
输出 #1复制
2 A001MP199 A111MP199
说明/提示
Time limit: 1 s, Memory limit: 128 MB.
思路
题目大意就是输入一个长度为9的模板以及 n 组数据 , 输出所有的满足条件的输入数据总数及这个满足条件的数据(可能有多个)。
一边输入一边对比模板,遇见 * 的符号则跳过不对比这个字符,如果所有的一遍扫过去都没有问题则记录答案(让ans+1) 。
代码实现(c++)
无注释:
#include<bits/stdc++.h>
using namespace std;
string a,c[1001];
long long n, ans=0;
void solve(long long n){
for(int i=1;i<=n;i++){
string b;
cin>>b;
int x=0;
for(int j=0;j<9;j++){
if(a[j]=='*'){
x++;
continue;
}
if(b[j]!=a[j]) break;
else x++;
}
if(x==9){
ans++;
c[ans]=b;
}
}
}
int main(){
cin>>a;
cin>>n;
solve(n);
cout<<ans<<"\n";
for(int i=1;i<=ans;i++) cout<<c[i]<<"\n";
return 0;
}
有注释:
#include<bits/stdc++.h>
using namespace std;
string a,c[1001]; //a表示模板格式, c记录答案
long long n, ans=0; //n表示有多少个输入的样例,ans是用来计数的
void solve(long long n){ //创建一个"解决"函数(就是对比)
for(int i=1;i<=n;i++){ //循环n次
string b; //b表示每次的输入
cin>>b; //输入
int x=0; //记录当前位置的
for(int j=0;j<9;j++){ //题目说明字符串长度为9
if(a[j]=='*'){ //如果是'*',直接跳过就行了
x++;
continue;
}
if(b[j]!=a[j]) break; //如果对比后发现不一样,直接跳出循环
else x++; //如果是一样的,就直接查下一个
}
if(x==9){ //循环结束后,如果没有因为不一样而先跳出循环
ans++; //就把ans+1,记录当前位置
c[ans]=b; //把符合的字符串记录下来
}
}
}
int main(){
cin>>a;
cin>>n;
solve(n); //循环n次
cout<<ans<<"\n"; //输出符合的字符串数量("\n"就是换行的意思)
for(int i=1;i<=ans;i++) cout<<c[i]<<"\n"; //输出被存进来的字符串
return 0; //你学会了吗?
}
留个赞吧