zoukankan      html  css  js  c++  java
  • HDU 4468 Spy(KMP+贪心)(2012 Asia Chengdu Regional Contest)

    Description

    “Be subtle! Be subtle! And use your spies for every kind of business. ”
    ― Sun Tzu
    “A spy with insufficient ability really sucks”
    ― An anonymous general who lost the war
    You, a general, following Sun Tzu’s instruction, make heavy use of spies and agents to gain information secretly in order to win the war (and return home to get married, what a flag you set up). However, the so-called “secret message” brought back by your spy, is in fact encrypted, forcing yourself into making deep study of message encryption employed by your enemy.
    Finally you found how your enemy encrypts message. The original message, namely s, consists of lowercase Latin alphabets. Then the following steps would be taken:
    * Step 1: Let r = s
    * Step 2: Remove r’s suffix (may be empty) whose length is less than length of s and append s to r. More precisely, firstly donate r[1...n], s[1...m], then an integer i is chosen, satisfying i ≤ n, n - i < m, and we make our new r = r[1...i] + s[1...m]. This step might be taken for several times or not be taken at all.
    What your spy brought back is the encrypted message r, you should solve for the minimal possible length of s (which is enough for your tactical actions).
     

    Input

    There are several test cases.
    For each test case there is a single line containing only one string r (The length of r does not exceed 105). You may assume that the input contains no more than 2 × 106 characters.
    Input is terminated by EOF.
     

    Output

    For each test case, output one line “Case X: Y” where X is the test case number (starting from 1) and Y is the desired answer.
    代码(46MS):
     1 #include <cstdio>
     2 #include <iostream>
     3 #include <algorithm>
     4 #include <cstring>
     5 using namespace std;
     6 
     7 const int MAXN = 100010;
     8 
     9 char r[MAXN], s[MAXN];
    10 int fail[MAXN];
    11 int n;
    12 
    13 int main() {
    14     int test = 0;
    15     while(scanf("%s", r) != EOF) {
    16         int n = strlen(r), m = 1, last = 1;
    17         memset(s, 0, sizeof(s));
    18         s[0] = r[0];
    19         for(int i = 1, j = 0; i < n; ++i) {
    20             while(j && r[i] != s[j]) j = fail[j];
    21             if(r[i] == s[j]) ++j;
    22             if(!j) {
    23                 for(int k = last; k <= i; ++k) {
    24                     s[m] = r[k];
    25                     int t = fail[m];
    26                     while(t && s[m] != s[t]) t = fail[t];
    27                     fail[m + 1] = t + (s[m] == s[t]);
    28                     ++m;
    29                 }
    30                 j = m;
    31             }
    32             if(j == m) last = i + 1;
    33         }
    34         printf("Case %d: %d
    ", ++test, m + n - last);
    35     }
    36 }
    View Code

  • 相关阅读:
    庆贺自己的软件入围中国软件创新大赛,可惜精英奖没有评上。一个笔记本飞了,哈哈,大奖500万呢!
    VMware vSphere开发(2)配置VMware vSphere Web Services SDK的开发环境
    IPhone 视图切换的的2种方法
    Windows系统加固方案
    我们公司原来C++招聘考试题,题目难度正常,没有稀奇古怪的题,如果答对60分以上,恭喜你基本算一个合格的网络开发工程师了。
    算法分析整数划分
    Java反射机制
    Java类集
    设计包含min函数的栈
    ASP.NET学习之Membership
  • 原文地址:https://www.cnblogs.com/oyking/p/3377803.html
Copyright © 2011-2022 走看看