poj 字符串相关之3038

来源:互联网 发布:福大图书馆数据库 编辑:程序博客网 时间:2024/06/05 20:20

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 100010using 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 100010using 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