POJ 3080 Blue Jeans 三種暴力法

來源:互聯網
上載者:User

標籤:使用   os   io   for   art   ar   時間   amp   

本題可以使用暴力法直接求解,思路也挺簡單的,不過實現起來也挺麻煩的。

本題最暴力直接使用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;}


使用strstr也可以,時間效率是一樣的:

#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;}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.