题目描述:
福尔摩斯从X星收到一份资料,全部是小写字母组成。他的助手提供了另一份资料:许多长度为8的密码列表。福尔摩斯发现,这些密码是被打乱后隐藏在先前那份资料中的。
请你编写一个程序,从第一份资料中搜索可能隐藏密码的位置。要考虑密码的所有排列可能性。
输入:
输入第一行:一个字符串s,全部由小写字母组成,长度小于1024*1024
紧接着一行是一个整数n,表示以下有n行密码,1<=n<=1000
紧接着是n行字符串,都是小写字母组成,长度都为8
输出:
一个整数, 表示每行密码的所有排列在s中匹配次数的总和。
样例输入
aaaabbbbaabbcccc
2
aaaabbbb
abcabccc
样例输出
4
#include<bits/stdc++.h>
using namespace std;
string s;
int n;
string s1;
map<string,int> maps;
int main()
{
cin>>s>>n;
int len=s.length();
for(int i=0;i+7<len;i++)//i+8错误,应为i+7
{
string temp;
for(int j=i;j<i+8;j++)
{
temp+=s[j];
sort(temp.begin(),temp.end());
maps[temp]++;
}
}
int ans=0;
while(n--)
{
cin>>s1;
sort(s1.begin(),s1.end());
//if(maps.count(s1))ans++;
if(maps.count(s1))ans+=maps[s1];
}
cout<<ans<<endl;
return 0;
}
3.23