5 条题解

  • 0
    @ 2024-11-23 9:40:30

    对于一个字符串的子串最多有n*(n+1)/2,这题的数据只有500,最多只有250000个子串,可以考虑用map存下所以子串的个数(由于map会对字符串自动排序,可能会很慢,所以字符串的题还是用 unordered_map吧),暴力判断即可,时间复杂度O(1)~O(n),代码很短

    #include<bits/stdc++.h>
    using namespace std;
    int const N=1e5+10;
    unordered_map<string,int> mp;
    int main()
    {
    	int n;
    	cin>>n;
    	string s1;
    	cin>>s1;
    	for(int i=0;i<n;i++)
    	{
    		string s="";
    		for(int j=i;j<n;j++)
    		{
    			s+=s1[j];
    			mp[s]++;
    		}
    	}
    	int m,ans=0;
    	cin>>m;
    	for(int i=1;i<=m;i++)
    	{
    		string s;
    		cin>>s;
    		ans+=mp[s];
    	}
    	cout<<ans<<endl;
    	return 0;
    }
    

    信息

    ID
    958
    时间
    1000ms
    内存
    256MiB
    难度
    6
    标签
    (无)
    递交数
    161
    已通过
    50
    上传者