POJ 3080 Blue Jeans 三种暴力法
来源:互联网 发布:数码宝贝数据库 编辑:程序博客网 时间:2024/05/16 16:16
本题可以使用暴力法直接求解,思路也挺简单的,不过实现起来也挺麻烦的。
本题最暴力直接使用strstr过。 这里使用hash表的方法过,这种方法好像有个学名的,主要思路就是把一个需要查找的字符串赋予一个数值,那么就可以把一串字符串的比较转换为一个值的比较了,那么就可以加速字符串的查找了。
#include <stdio.h>#include <string.h>#include <stdlib.h>const long long MOD = (int)(1E9+7);//如果增加mod的话,会有很大几率出错的,本题就不能增加MOD,否则一直WA,不使用MOD就马上AC了const int MAX_N = 61;const int MAX_M = 11;const int ALP_LEN = 4;const int SIGNIFICANT = 3;const char *intToChar = "ATGC";short charToInt[256];char dict[MAX_M][MAX_N];char subStr[MAX_N];long long finger;bool searchFinger(char *txt, int L, int len){long long F = 0;long long K = 1;int i = 0;for (; i < L; i++){F = ((F*5)+charToInt[txt[i]]);K = K*5;}if (F == finger) return true;for (int j = 0; i < len; j++, i++){F = ((F*5)+charToInt[txt[i]]);F = (F-K*charToInt[txt[j]]);if (F == finger) return true;}return false;}int main(){charToInt['A'] = 1;//不能从0开始,如果使用hash法charToInt['T'] = 2;charToInt['G'] = 3;charToInt['C'] = 4;int T, n;scanf("%d", &T);while (T--){scanf("%d", &n);getchar(); // get rid of '\n'for (int i = 0; i < n; i++){gets(dict[i]);}int len = MAX_N - 1;int maxLen = 0;char res[MAX_N] = {'\0'};for (int i = 0; i < len; i++)//search every start position{finger = 0;for (int L = 1; L <= len-i; L++)//every substring{subStr[L-1] = dict[0][i+L-1];subStr[L] = '\0';finger = ((finger*5)+(charToInt[subStr[L-1]]));int j = 1;for (; j < n; j++){if (!searchFinger(dict[j], L, len)) break;}if (j != n) break;//break to the next start positionif (L > maxLen || (L==maxLen && strcmp(res, subStr)>0)){//alphabetic firststrcpy(res, subStr);maxLen = L;}}}if (maxLen < SIGNIFICANT){puts("no significant commonalities");}else{printf("%s\n", res);}}return 0;}
#include <stdio.h>#include <string.h>#include <stdlib.h>const int MAX_N = 61;const int MAX_M = 11;const int ALP_LEN = 4;const int SIGNIFICANT = 3;char dict[MAX_M][MAX_N];char subStr[MAX_N];int main(){int T, n;scanf("%d", &T);while (T--){scanf("%d", &n);getchar(); // get rid of '\n'for (int i = 0; i < n; i++){gets(dict[i]);}int len = MAX_N - 1;int maxLen = 0;char res[MAX_N] = {'\0'};for (int i = 0; i < len; i++)//search every start position{for (int L = 1; L <= len-i; L++)//every substring{subStr[L-1] = dict[0][i+L-1];subStr[L] = '\0';int j = 1;for (; j < n; j++){if (!strstr(dict[j], subStr)) break;}if (j != n) break;//break to the next start positionif (L > maxLen || (L==maxLen && strcmp(res, subStr)>0)){//alphabetic firststrcpy(res, subStr);maxLen = L;}}}if (maxLen < SIGNIFICANT){puts("no significant commonalities");}else{printf("%s\n", res);}}return 0;}
使用strstr的第二种方法:
const int MAX_N = 61;const int MAX_M = 11;const int ALP_LEN = 4;const int SIGNIFICANT = 3;char dict[MAX_M][MAX_N];char subStr[MAX_N];int main(){int T, n;scanf("%d", &T);while (T--){scanf("%d", &n);getchar(); // get rid of '\n'for (int i = 0; i < n; i++){gets(dict[i]);}int len = MAX_N - 1;int maxLen = 0;char res[MAX_N];for (int i = 0; i < len; i++)//search every start position{int len2 = len-i;strncpy(subStr, dict[0]+i, len2);for (int L = len2; L > 0; L--)//every substring{subStr[L] = '\0';int j = 1;for (; j < n; j++){if (!strstr(dict[j], subStr)) break;}if (j == n){if (L > maxLen || (L==maxLen && strcmp(res, subStr)>0)){//alphabetic firststrcpy(res, subStr);maxLen = L;}break;//break to the next start position}}}if (maxLen < SIGNIFICANT){puts("no significant commonalities");}else{printf("%s\n", res);}}return 0;}
1 0
- POJ 3080 Blue Jeans 三种暴力法
- poj 3080 Blue Jeans 暴力
- [KMP或者暴力]POJ 3080 Blue Jeans
- POJ 3080 Blue Jeans 暴力枚举+KMP
- POJ 3080 Blue Jeans KMP+暴力
- poj 3080 Blue Jeans (kmp暴力)
- POJ 3080 Blue Jeans (KMP || 暴力)
- POJ 3080 Blue Jeans(暴力)
- poj-3080-Blue Jeans-串-暴力
- POJ 3080 Blue Jeans kmp+暴力枚举
- poj 3080 Blue Jeans 【KMP 暴力枚举】
- POJ 3080--Blue Jeans【KMP && 暴力枚举】
- poj 3080 Blue Jeans 字符串,暴力
- POJ 3080 Blue Jeans (暴力)
- poj 3080 Blue Jeans 【kmp+暴力】
- POJ 3080 Blue Jeans (很暴力)
- POJ-3080 Blue Jeans(纯暴力)
- POJ 3080 Blue Jeans 串的暴力
- Linux apt-get
- [ZOJ 3352] Boring Board Game [动态规划]
- 用于unity3d的带有延迟功能的消息机
- 装饰模式
- CF 259div2 D (状态压缩dp)
- POJ 3080 Blue Jeans 三种暴力法
- LA3938Ray, Pass me the Dishes, LA 3938 , 线段树,区间查询
- 插入排序(算法学习系列)
- git_上传项目代码到github
- Installation error: INSTALL_FAILED_INSUFFICIENT_STORAGE
- poj2104 K-th Number 主席树或划分树
- hdu 1171 多重背包
- fragment
- [ZOJ 3358] Green Dam Girl [动态规划+Floyd]