[Blue Bridge Cup] 2015 final ciphertext search (map)

Title description

Holmes received a document from Star X, all in lowercase letters.
His assistant provided another piece of information: many password lists of length 8.
Holmes discovered that these passwords had been shuffled and hidden in the previous data.
Please write a program to search for possible hidden password locations from the first document.
Consider all possible permutations of passwords.

enter

Enter the first line: a string s, all composed of lowercase letters, length less than 1024*1024,
followed by an integer n, indicating that there are n lines of passwords, 1<=n<=1000
followed by n lines of strings , Are composed of lowercase letters, with a length of 8

Output

An integer representing the total number of matches in s for all permutations of each row of passwords.

Sample input

aaaabbbbaabbcccc
2
aaaabbbb
abcabccc

Sample output

4

prompt

The first password was matched 3 times, and the second password was matched 1 time, a total of 4 times.


Code:

Because the password can be all arranged, as long as the number of characters that have appeared is the same as a certain eight digits in s, it represents an answer.

#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
typedef long long ll;
string s,t;
map<string,int> mp;
ll n,ans=0;
 
int main() 
{
    
    
	cin>>s>>n;

	for(int i=0;i<=s.size()-8;i++)
	{
    
    
		string x=s.substr(i,8); //每次切8个
		sort(x.begin(),x.end()); //排序
		mp[x]++; //记录出现次数
	}
	
	for(int i=0;i<n;i++)
	{
    
    
		cin>>t;
		sort(t.begin(),t.end()); //排序
		ans+=mp[t]; //加上出现过的次数
	}
	
	cout<<ans;
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45260385/article/details/109406630