USACO 1.3.3 Calf Flac (calfflac)

來源:互聯網
上載者:User

1、比較愚蠢的暴力搜尋:兩個指標分別從從第一個和最後一個開始,迴圈往中間移動,判斷兩個指標之間的字串是否是迴文數。重複搜尋過多,導致最後一個測試資料逾時:(

/*<br />ID: gengjia1<br />LANG: C<br />TASK: calfflac<br />*/<br />#include <stdio.h><br />#include <stdlib.h><br />#include <string.h><br />#include <ctype.h><br />//#define NDEBUG<br />#include <assert.h></p><p>#define MAX_LEN 20000<br />int count;</p><p>struct str{<br />int len;<br />char s[MAX_LEN + 1];<br />};</p><p>struct pal{<br />int len;<br />int begin;<br />int end;<br />};</p><p>int letter(const char a)<br />{<br />if((a >= 'a' && a <= 'z') ||<br />(a >= 'A' && a <= 'Z'))<br />return 1;<br />return 0;<br />}</p><p>int main(void) {<br />FILE *fin = fopen ("calfflac.in", "r");<br />FILE *fout = fopen ("calfflac.out", "w");<br />assert(fin != NULL && fout != NULL);</p><p>struct str s = {0};<br />struct pal pa = {0};<br />char tmp[80] = {0};<br />char t[MAX_LEN] = {0};<br />int curlen = 0;<br />int i, j, k, p, q;</p><p>while( fgets (tmp, 80, fin) != NULL)<br />{<br />strcat(s.s, tmp);<br />}<br />s.len = strlen(s.s);</p><p>for(i = 0; i < s.len; i++)<br />{<br />if(letter(s.s[i]) == 0)<br />continue;<br />for(j = s.len-1; j > i; j--)<br />{<br />if(letter(s.s[j]) == 0)<br />continue;<br />curlen = 0;<br />for(p = i, q = j; p < q; )<br />{<br />if(letter(s.s[p]) == 0)<br />{<br />p++;<br />continue;<br />}<br />if(letter(s.s[q]) == 0)<br />{<br />q--;<br />continue;<br />}<br />if(tolower(s.s[p]) == tolower(s.s[q]))<br />{<br />curlen += 2;<br />p++;<br />q--;<br />}<br />else<br />{<br />curlen = 0;<br />break;<br />}<br />}<br />if(p == q && letter(s.s[p]) == 1)<br />curlen += 1;</p><p>if(curlen > pa.len)<br />{<br />pa.len = curlen;<br />pa.begin = i;<br />pa.end = j;<br />}<br />}<br />}<br />fprintf(fout, "%d/n", pa.len);<br />for(i = pa.begin; i <= pa.end; i++)<br />{<br />fprintf(fout, "%c", s.s[i]);<br />}<br />fprintf(fout, "/n");</p><p>fclose(fin);<br />fclose(fout);<br />exit(0);<br />}<br />

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.