[UVA][字串理] 10438 - Meta Editor@Morris' Blog|PChome Online 人新台
2013-01-21 08:25:40| 人662| 回1 | 上一篇 | 下一篇

[UVA][字串理] 10438 - Meta Editor

0 收藏 0 0 站台

Brightness of Brain Contest
Problem J
Time limit: 1 second Memory: 16 MB

Meta Editor

The Problem

    You got a job as a programmer in the ACM (A Cool Meta-editor) company, which has has been serving as a producer of one of the coolest text editor of the world. In the update process of the text editor program of that company you are assigned a job to make a program unit which will grammatically correct a line of text. Your job is very simple, take a line of text as input and produce a line of output without the words repeated.

The Input

    Contains several lines of text. Each line contains several English words having no more then 50 characters each. A word may be defined as a portion of a string separated by one or more spaces or tabs. There will be no blank line in the input. A line may contain at most 20,000 characters. Input is terminated by EOF.

The Output

    Produce a line of text for each line of input, after removing any repeated pattern. The words must be separated by only one space.

Sample Input

test string test string test string repeat repeat 

Sample Output

test string repeat 


很困,在於理解目容上。
首先要找到他要 reduce 的部分,按照序去找可以短的部分。
假 substring pa, pb, A

如果存在 pa A A pb 成 pa A pb。

#include <stdio.h>
#include <string.h>
#define maxN 20005
char line[maxN], *token[maxN];
int main() {
while(gets(line)) {
int n = 0, i, j, k;
for(i = 0; line[i]; i++) {
if(line[i] > 32) {
token[n++] = line+i;
while(line[i] > 32)
i++;
if(line[i] == '\0')
break;
line[i] = '\0';
}
}
while(1) {
int flag = 0;
for(i = 0; i < n; i++) {
for(j = i+1; j < n; j++) {
int len = j-i; // [i...j-1],[j...j+len-1]
if(j+len-1 >= n) break;
for(k = 0; k < len; k++)
if(strcmp(token[i+k], token[j+k]))
break;
if(k == len) {
for(k = j+len, j = i+len; k < n; k++, j++)
token[j] = token[k];
flag = 1, i = j = n;
n -= len;
}
}
}
if(!flag) break;
}
for(i = 0; i < n; i++) {
if(i) putchar(' ');
printf("%s", token[i]);
}
puts("");
}
return 0;
}



台: Morris
人(662) | 回(1)| 推 (0)| 收藏 (0)|
全站分: 不分 | 人分: UVA |
此分下一篇:[UVA][二分搜+greedy] 907 - Winterim Backpacking Trip
此分上一篇:[UVA][何] 10613 - Mushroom Misery

asas(不想目阿QAQ)
再次感大大,大生活少不了上...XD
2013-01-22 22:22:15
是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能0) 
(有*必填)
TOP
全文
ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86