White cattle off season May 18 B.Forsaken solution to a problem like string (hash)

Topic Link

Topic ideas

Preface
to see a good number of cumulative number of topics, click on Mongolia, in fact, a closer look at the meaning of the questions still have to understand

The question
for each inquiry, in essence, is to strive for the length of all the other strings as a substring len is the number of sub-string string x, then multiplied by the total number of len is the answer, noting k very small, so we need only able to save All hash value for each string, and records the number of times the hash value occurs, then the number of times each query hash value that appears on the list. Complexity is O (nk ^ 2 + qk)

Code

Direct stl

#include<iostream>
#include<map>
using namespace std;
typedef long long ll;
const int maxn=5e4+5;
int n,k,q,x,len;
ll ans;
string s[maxn];
map<string,int> cnt;
int main(){
	ios::sync_with_stdio(false);//加速读入 
	cin.tie(0),cout.tie(0);
	cin>>n>>k;
	for(int i=1;i<=n;i++){
		cin>>s[i];
		for(int len=1;len<=k;len++){//字串的长度 
			for(int j=0;j+len<=k;j++){//起点 
				cnt[s[i].substr(j,len)]++;
			}
		}
	}
	cin>>q;
	while(q--){
		ans=0;
		cin>>x>>len;
		map<string,int> me;//重新定义了则不需要清空 
		for(int i=0;i+len<=k;i++){
			me[s[x].substr(i,len)]++;
		}
		for(int i=0;i+len<=k;i++){
			ans+=cnt[s[x].substr(i,len)]-me[s[x].substr(i,len)];	
		}
		cout<<ans*len<<endl;
	}
	return 0;
} 
Published 68 original articles · won praise 2 · Views 2241

Guess you like

Origin blog.csdn.net/m0_46209312/article/details/105277271