当前位置:软件学习 > Word >>

CF 319D(Have You Ever Heard About the Word?-模拟)

D. Have You Ever Heard About the Word?
time limit per test6 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
A substring of a string is a contiguous subsequence of that string. So, string bca is substring of string abcabc, but string cc is not.

A repeating block is a string formed by concatenating some string with itself. So, string abcabc is a repeating block, but strings abcabd,ababab are not.

You've got a sequence of Latin characters (string). At each step you find the shortest substring that is a repeating block, if there exists more than one you must choose the leftmost. As the substring is of form XX (X — some string) you replace this substring with X, in other words you delete one of the X substrings in the substring. You repeat this process until there remains no repeating block in the string.

How would the final string looks like? Look at the sample explanation to understand the statement more precise.

Input
In the first line of input you're given a string of small Latin characters with length between 1 to 50000, inclusive.

Output
Print the final string after applying changes.

Sample test(s)
input
abccabc
output
abc
input
aaaabaaab
output
ab
input
birdbirdbirdistheword
output
birdistheword
Note
At the first sample the string transforms as follows: abccabc  →  abcabc  →  abc.

At the second sample the string transforms as follows: aaaabaaab  →  aaabaaab  →  aabaaab  →  abaaab  →  abaab  →  abab  →  ab.

 

模拟能过……

E文是硬伤,要的是最短中最左子串,不是最左中最短子串……


[cpp]
#include<cstdio>  
#include<cstdlib>  
#include<cstring>  
#include<iostream>  
#include<algorithm>  
#include<functional>  
#include<cmath>  
#include<cctype>  
using namespace std; 
#define For(i,n) for(int i=1;i<=n;i++)  
#define Rep(i,n) for(int i=0;i<n;i++)  
#define Fork(i,k,n) for(int i=k;i<=n;i++)  
#define ForD(i,n) for(int i=n;i;i--)  
#define Forp(x) for(int p=pre[x];p;p=next[p])  
#define RepD(i,n) for(int i=n;i>=0;i--)  
#define MEM(a) memset(a,0,sizeof(a))  
#define MEMI(a) memset(a,127,sizeof(a))  
#define MEMi(a) memset(a,128,sizeof(a))  
#define MAXN (50000+10)  
char s[MAXN]; 
int main() 

//  freopen("CF319D.in","r",stdin);  
//  freopen(".out","w",stdout);  
    int t=1; 
    while (t--) 
    { 
    int n=strlen(gets(s+1)); 
    For(len,n/2) 
    { 
        int tot=0; 
        For(j,n-len) 
        { 
            if (s[j]==s[j+len]) tot++;else tot=0; 
            if (tot==len) 
            { 
                Fork(k,j+1,n-len) s[k]=s[k+len]; 
                n-=len;len=0;break; 
            } 
        }            
    } 
    s[n+1]=0; 
    puts(s+1); 
    } 
    return 0; 

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#include<cmath>
#include<cctype>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define RepD(i,n) for(int i=n;i>=0;i--)
#define MEM(a) memset(a,0,sizeof(a))
#define MEMI(a) memset(a,127,sizeof(a))
#define MEMi(a) memset(a,128,sizeof(a))
#define MAXN (50000+10)
char s[MAXN];
int main()
{
// freopen("CF319D.in","r",stdin);
// freopen(".out","w",stdout);
 int t=1;
 while (t--)
 {
 int n=strlen(gets(s+1));
 For(len,n/2)
 {
  int tot=0;
  For(j,n-len)
  {
   if (s[j]==s[j+len]) tot++;else tot=0;
   if (tot==len)
   {
    Fork(k,j+1,n-len) s[k]=s[k+len];
    n-=len;len=0;break;
   }
  }   
 }
 s[n+1]=0;
 puts(s+1);
 }
 return 0;
}

 

 


 

补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,