zoukankan      html  css  js  c++  java
  • UVa 11475 Extend to Palindrome

    方法:kmp

    其实这道题算是一个字符串匹配,原string 为 str, 把str 反转后得到 rev,看rev在str什么位置可以匹配到,多出的部分不算。匹配的方法有很多种,这道题用kmp比较容易。也可以用suffix array 或者 hash。

    kmp code:

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <iostream>
    #include <string>
    #include <vector>
    #include <stack>
    #include <bitset>
    #include <cstdlib>
    #include <cmath>
    #include <set>
    #include <list>
    #include <deque>
    #include <map>
    #include <queue>
    #include <fstream>
    #include <cassert>
    #include <unordered_map>
    #include <cmath>
    #include <sstream>
    #include <time.h>
    #include <complex>
    #include <iomanip>
    #define Max(a,b) ((a)>(b)?(a):(b))
    #define Min(a,b) ((a)<(b)?(a):(b))
    #define FOR(a,b,c) for (ll (a)=(b);(a)<(c);++(a))
    #define FORN(a,b,c) for (ll (a)=(b);(a)<=(c);++(a))
    #define DFOR(a,b,c) for (ll (a)=(b);(a)>=(c);--(a))
    #define FORSQ(a,b,c) for (ll (a)=(b);(a)*(a)<=(c);++(a))
    #define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a))
    #define FOREACH(a,b) for (auto &(a) : (b))
    #define rep(i,n) FOR(i,0,n)
    #define repn(i,n) FORN(i,1,n)
    #define drep(i,n) DFOR(i,n-1,0)
    #define drepn(i,n) DFOR(i,n,1)
    #define MAX(a,b) a = Max(a,b)
    #define MIN(a,b) a = Min(a,b)
    #define SQR(x) ((LL)(x) * (x))
    #define Reset(a,b) memset(a,b,sizeof(a))
    #define fi first
    #define se second
    #define mp make_pair
    #define pb push_back
    #define all(v) v.begin(),v.end()
    #define ALLA(arr,sz) arr,arr+sz
    #define SIZE(v) (int)v.size()
    #define SORT(v) sort(all(v))
    #define REVERSE(v) reverse(ALL(v))
    #define SORTA(arr,sz) sort(ALLA(arr,sz))
    #define REVERSEA(arr,sz) reverse(ALLA(arr,sz))
    #define PERMUTE next_permutation
    #define TC(t) while(t--)
    #define forever for(;;)
    #define PINF 1000000000000
    #define newline '
    '
    
    #define test if(1)if(0)cerr
    using namespace std;
      using namespace std;
    typedef vector<int> vi;
    typedef vector<vi> vvi;
    typedef pair<int,int> ii;
    typedef pair<double,double> dd;
    typedef pair<char,char> cc;
    typedef vector<ii> vii;
    typedef long long ll;
    typedef unsigned long long ull;
    typedef pair<ll, ll> l4;
    const double pi = acos(-1.0);
    
    
    string str, rev;
    void getfail(const string &str, int *f)
    {
        int m = str.length();
        f[0] = f[1] = 0;
        for (int i = 1; i < m; ++i)
        {
            int j = f[i];
            while (j && str[i] != str[j]) j = f[j];
            f[i+1] = str[i]==str[j]?j+1:0;
        }
    }
    int f[100001];
    int main()
    {
        while (cin >> str)
        {
            rev = str;
            reverse(rev.begin(), rev.end());
            getfail(rev, f);
            int ans;
            int n = str.length();
            int j = 0;
            for (int i = 0; i < n; ++i)
            {
                while (j && rev[j] != str[i]) j = f[j];
                if (rev[j] == str[i]) ++j;
                //cerr << str.substr(0,i+1) << " " << j << newline;
            }
            cout << str << rev.substr(j) << newline;
        }
    }
    

     

  • 相关阅读:
    学习:GridView中asp:BoundField的Visible=false时,无法取到这个字段的值
    C#读、写、删除注册表
    ERROR [IM002] [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序
    Gridview隐藏列的取值问题
    SQL 2005导入EXCEL
    GridView使用LinkButton和Button两种方式的删除确认
    (转)ASP.Net的AccessDataSource设置错误"未将对象引用设置到对象的实例"的解决方案
    给GridView文本加上边框
    (转)从客户端中检测到有潜在危险的 Request.Form 值
    (转)Asp.net中的ServerVariables集合
  • 原文地址:https://www.cnblogs.com/skyette/p/6358316.html
Copyright © 2011-2022 走看看