poj3080

字符串的题嘛,水的很,STL解决!很方便。ps,npos比跟.size()比好得多,不必考虑边界问题。这代码效率很低,n^3的

#include <iostream>
#include <string>

#define MAXN 60

using namespace std;


int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        
        string str[15];
        
        for(int i = 0; i < n; i++)
        {
            cin >> str[i];
        }
        
        string res = "";
        
        for(int i = 3; i <= MAXN; i++)
        {
            for(int j = 0; j <= MAXN - i; j++)
            {
                string tmp = str[0].substr(j, i);
                bool flag = true;
                for(int k = 1; k < n; k++)
                {
                    if(str[k].find(tmp) == string::npos)
                    {
                        flag = false;
                        break;
                    }
                }
                if(flag && res.size() < tmp.size())
                    res = tmp;
                else if(flag && tmp.size() == res.size() && tmp < res)
                    res = tmp;
            }
        }
        if(res == "")
            cout <<"no significant commonalities" << endl;
        else
            cout << res << endl;
    }
    return 0;
}



poj3080

上一篇:数据结构 -- 二叉树


下一篇:java.lang.OutOfMemoryError: unable to create new native thread