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
阅读全文
0 0
- poj 字符串相关之3038
- poj 字符串相关之1200 Crazy Search
- poj 字符串相关之1204 Word Puzzles
- poj 字符串相关之1816 Wild Words
- poj 字符串相关之1002 487-3279
- poj 字符串相关之2513 Colored Sticks
- R之字符串相关
- R之字符串相关
- JS之字符串相关
- poj 字符串相关的水题
- poj之3671 字符串排序
- poj union_find相关之1611 The Suspects
- poj dfs相关之1154 LETTERS
- poj dfs相关之1011 Sticks
- poj dfs相关之1321 棋盘问题
- poj dfs相关之3984 迷宫问题
- poj dfs相关之1753 Flip Game
- poj 贪心相关之1328 Radar Installation
- Android学习之Service(下)
- shellScript之break关键字
- 机器学习笔记十八 FP-growth
- mysql中Invalid default value for 'stime'问题
- Laravel 5.4 官方文档摘记:路由
- poj 字符串相关之3038
- 决策树与随机森林算法(理论)-----------------------机器学习系列(二))
- mysql 得到的数据插入另一个新表中
- 机器学习笔记十九 使用PCA简化数据(未完)
- 树链剖分(bzoj 1036: [ZJOI2008]树的统计Count)
- 机器学习笔记二十 利用SVD简化数据
- 51Nod 1717 好数&&如何判断一个数是完全平方数
- Ubuntu16 下创建文件时没有权限
- 【操作系统】-服务器操作系统