zoukankan      html  css  js  c++  java
  • poj 3080 hdu 1238 暴力KMP解决

    poj 3080 hdu 1238 暴力KMP解决

    poj 3080,<—原题链接

    hdu 1238,<—原题链接

    题意

    poj 3080 是说给你n个字符串,找出他们共有的最长字符串,但是如果找到的字符串长度小于3,也算失败,并输出相应的语句,否者查找成功,输出找到的字符串。

    hdu 1238和上边的差不多,不同的是,找到的这个字符串R有可能不是这个n个字符串中某个字符串T的子串,但是如果R的逆串R'T的子串的话,也算成功。

    解题思路

    这个题好像没看到什么更有效的方法,暴力枚举,数据量也比较小。

    对于这两个题,我们的做法都是直接按照第一个字符串枚举所有的子串,然后看看剩下的字符串是不是都含有这个字符串,就这么简单,就这么直接,勇敢枚举吧少年。

    代码实现

    这两个代码真的是极其相似

    //poj 3080
    #include<cmath>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<iostream>
    #include<string>
    #include<stack>
    #include<queue>
    #include<map>
    using namespace std;
    const int MAXN=12;
    const int MAX_LEN=67;
    string str[MAXN];
    int m, nx[MAX_LEN];
    void get(string &s)
    {
    	int j=0, k=-1, len=s.length();
    	nx[0] = -1;
    	while(j<len)
    	{
    		if(k == -1 || s[j] == s[k])
    		{
    			j++; k++;
    			nx[j] = k; 
    		}
    		else k = nx[k];
    	}
    }
    bool KMP(string &s1, string &s2)
    {
    	int i=0, j=0, len1=s1.length(), len2=s2.length();
    	while(i<len1)
    	{
    		if(j == -1 || s1[i] == s2[j])
    		{
    			i++; j++;
    		}
    		else j = nx[j];
    		if(j == len2)
    			return true;
    	} 
    	return false;
    }
    int main()
    {
    	int t;
    	scanf("%d", &t);
    	while(t--)
    	{
    		string ans=""; 
    		scanf("%d",&m);
    		for(int i=0; i<m; i++)
    			cin >> str[i];
    		for(int k=1; k<=str[0].length(); k++)
    		{
    			for(int i=0; i + k <= str[0].length(); i++) //开始的位置 
    			{
    				string mod = str[0].substr(i, k);
    				get(mod);
    				int count = 1; //记录所有匹配的个数 
    				for(int j=1; j<m; j++)
    				{
    					if(KMP(str[j], mod) == true)
    						count++; 
    				}
    				if(count == m)
    				{
    					if(mod.length() > ans.length())
    						ans = mod;
    					else if( mod.length() == ans.length() )
    						ans = min(ans, mod);
    				}
    			}
    		}
    		if(ans.length() < 3)
    			printf("no significant commonalities
    ");
    		else cout<<ans<<endl;	
    	} 
    	return 0;
    }
    
    //hdu 1238
    #include<cmath>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<iostream>
    #include<string>
    #include<stack>
    #include<queue>
    #include<map>
    using namespace std;
    const int MAX_LEN=107;
    string str[MAX_LEN];
    int m, nx[MAX_LEN];
    void get(string &s)
    {
    	int j=0, k=-1, len=s.length();
    	nx[0] = -1;
    	while(j<len)
    	{
    		if(k == -1 || s[j] == s[k])
    		{
    			j++; k++;
    			nx[j] = k; 
    		}
    		else k = nx[k];
    	}
    }
    bool KMP(string &s1, string &s2)
    {
    	int i=0, j=0, len1=s1.length(), len2=s2.length();
    	while(i<len1)
    	{
    		if(j == -1 || s1[i] == s2[j])
    		{
    			i++; j++;
    		}
    		else j = nx[j];
    		if(j == len2)
    			return true;
    	} 
    	return false;
    }
    int main()
    {
    	int t;
    	scanf("%d", &t);
    	while(t--)
    	{
    		int ans=0;
    		scanf("%d",&m);
    		for(int i=0; i<m; i++)
    			cin >> str[i];
    		for(int k=1; k<=str[0].length(); k++)
    		{
    			for(int i=0; i + k <= str[0].length(); i++) //开始的位置 
    			{
    				string mod = str[0].substr(i, k);
    				get(mod);
    				int count = 1; //记录所有匹配的个数 
    				for(int j=1; j<m; j++)
    				{
    					if(KMP(str[j], mod) == true)
    						count++; 
    				}
    				if(count == m)
    				{
    					if(mod.length() > ans)
    						ans = mod.length();
    				}
    				count=1;
    				for(int j=1; j<m; j++)
    				{
    					reverse( str[j].begin(), str[j].end() );
    					if(KMP(str[j], mod) == true)
    						count++;
    				}
    				if(count == m)
    				{
    					if(mod.length() > ans)
    						ans = mod.length();
    				}
    			}
    		}
    		printf("%d
    ", ans);	
    	} 
    	return 0;
    }
    
    欢迎评论交流!
  • 相关阅读:
    18.5 推挽输出和开漏输出区别
    19.3 Table 1-2.S3C2440A 289-Pin FBGA Pin Assignments (Sheet 4 of 9) (Continued)
    19.2 MEMORY CONTROLLER
    19.1 PORT CONTROL DESCRIPTIONS
    17.2 SourceInsight批量注释
    17.3 删除没用的project
    17.1 添加汇编文件并可索引
    16.2 在SecureCRT编写C程序不高亮显示
    16.1 解决SecureCRT的Home+End+Del不好用使用方法
    15.1 打开文件时的提示(不是dos格式)去掉头文件
  • 原文地址:https://www.cnblogs.com/alking1001/p/12247748.html
Copyright © 2011-2022 走看看