poj 字符串相关之3038

poj 字符串相关之3038
注意审题 啊
For each dataset in the input, output the longest base subsequence common to all of the given base sequences. If the longest common subsequence is less than three bases in length, display the string “no significant commonalities” instead. If multiple subsequences of the same longest length exist, output only the subsequence that comes first in alphabetical order.

以下的代码超时,我猜测可能是cin的原因还有判断子串的时候效率太低吧

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
//#include<math.h>
#include<algorithm>
#include<vector>
#include<map>
#define MAXNUM 100010
using namespace std;
int n;
string str[12];
bool check(int t,string substr)
{
    int i, len;
    bool mark;
    mark = 0;
    len = substr.length();
    for (i = 0; i <= str[t].length() - len; i++)
    {
        if (str[t].substr(i, len) == substr)
        {
            mark = 1;
            break;
        }
    }
    if (mark)
        return true;
    else
        return false;
}
int main(void)
{
    //freopen("1.txt", "r", stdin);
    bool flag;
    int i, j, k, len, cases, count;
    string result, substr;
    cin >> cases;
    while (cases > 0)
    {
        cin >> n;
        result = "";
        for (i = 0; i < n; i++)
        {
            cin >> str[i];
        }
        len = str[0].length();
        for (i = len; i >= 3; i--)//58
        {
            result = "";
            flag = 0;
            for (k = 0; k <= len - i; k++)
            {
                count = 1;
                substr = str[0].substr(k, i);
                for (j = 1; j < n; j++)
                {
                    if (!check(j, substr))
                        break;
                    else
                        count++;
                }
                if (count == n)
                {
                    flag = 1;
                    if (result.empty() || substr < result)
                        result = substr;
                }
            }
            if (flag)
            {
                cout << result << endl;
                break;
            }
        }
        if (!flag)
            cout << "no significant commonalities" << endl;
        cases--;
    }

}

Accepted 140K 16MS
换用char数组和strstr等函数就可以ac了

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
//#include<math.h>
#include<algorithm>
#include<vector>
#include<map>
#define MAXNUM 100010
using namespace std;
int n;
char str[15][65], ans[65][65], substr[65];
int main(void)
{
    //freopen("1.txt", "r", stdin);
    bool flag;
    int i, j, k, len, cases, count;
    scanf("%d", &cases);
    while (cases > 0)
    {
        scanf("%d", &n);
        for (i = 0; i < n; i++)
            scanf("%s", str[i]);
        memset(ans, 0, sizeof(ans));
        count = 0;
        for (i = strlen(str[0]); i >= 3; i--)
        {
            for (j = 0; j <= strlen(str[0]) - i; j++)
            {
                memset(substr, 0, sizeof(substr));
                strncpy(substr, str[0] + j, i);
                for (k = 1; k < n; k++)
                {
                    if (!strstr(str[k], substr))
                        break;
                }
                if (k == n)
                    strcpy(ans[count++], substr);
            }
            if (count)
            {
                memset(substr, 0, sizeof(substr));
                strcpy(substr, ans[0]);
                for (i = 1; i < count;i++)
                if (strcmp(ans[i], substr) < 0)
                    strcpy(substr, ans[i]);
                printf("%s\n", substr);
                break;
            }
        }
        if (!count)
            printf("no significant commonalities\n");
        cases--;
    }

}

回顾一下一些char的函数
strlen 求char数组的长度
strstr(s1,s2)判断s2是否是s1的子串
strcpy(s,tmp)将tmp字符串赋值给s串
strncpy(s,tmp+1,len)将tmp串从1位置开始len长度的串赋值给s
strcmp(s1,s2)比较s1与s2的每一位,s1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值