zoukankan      html  css  js  c++  java
  • 武大OJ 622. Symmetrical

    Description

             Cyy likes something symmetrical, and Han Move likes something circular. Han Move gave you a circular string whose length is N, i.e. the i-th character of the string is the neighbor of the ( (i-1) mod N )-th character and ( (i+1) mod N )-th character.
             As Cyy likes something symmetrical, you want to find out whether this string can be symmetrical. There’s an operation for you to make the string symmetrical. This operation is called “disconnect”. You can use “disconnect” only once to break the link between any two neighbor characters, making the circular string into a linear string. If you can use “disconnect” to make this string into a palindrome, this string can be considered to be symmetrical.

             Please tell Cyy whether this circular string is symmetrical. 

    Input

    The input file consists of multiple test cases.
    For each test case, there’s only a string in a line to represent the circular string. The string only consists of lowercase characters. ( 1 <= N <= 65536 )
    It’s guaranteed that the sum of N is not larger than 1000000.
    

    Output

    For each test case, output “Yes” in a line if the circular string is symmetrical, output “No” otherwise.
    

    Sample Input

    aaaaaa
    abcde
    ababab
    aaaaba
    aabbaa
    

    Sample Output

    Yes
    No
    No
    No
    Yes



    一句话题意:循环字符串能否断开形成回文串
    循环的东西,很明显要直接复制一遍
    回文串,很明显manacher

    如果原串长度len为奇数,则找到的回文数长度m必须>=len且m为奇数

    如果原串长度len为偶数,则找到的回文数长度m必须>=len且m为偶数

     一开始总是超时,一直以为是判断输入结束不对,后来才发现是用了string,每次都用string生成!#a#b..,很耗时!

     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<math.h>
     4 #include<stdlib.h>
     5 #include<string.h> 
     6 #include<algorithm>
     7 using namespace std;
     8 
     9 int p[300000];
    10 bool manacher(char* s,int len){
    11     int length=0;
    12     int maxRight=1;
    13     int mid=1;
    14     int l=strlen(s);
    15     for(int i=1;i<l;++i)
    16     {
    17         if(i<maxRight)
    18         p[i]=min(p[2*mid-i],maxRight-i);
    19         else p[i]=1;
    20         
    21         while(s[i-p[i]]==s[i+p[i]]) p[i]++;
    22         
    23         if(p[i]+i>maxRight) 
    24         {
    25             maxRight=p[i]+i;
    26             mid=i;
    27         }
    28         length=p[i]-1;    
    29         if(len%2==0)
    30         {
    31             if(!(length<len||length%2==1))
    32             return true;
    33         }
    34         if(len%2==1)
    35         {
    36             if(!(length<len||length%2==0))
    37             return true;
    38         }
    39     }
    40     return false;
    41 
    42 }
    43 
    44 int main()
    45 {
    46     int len;
    47     char s[300000];
    48     
    49     while(~scanf("%s",&s))
    50     {
    51         len=strlen(s);
    52         for(int i=len;i>=0;--i)
    53         s[i+len]=s[i];
    54     //    cout<<s<<endl;
    55         for(int i=2*len;i>=0;--i)
    56         {
    57             s[2*i+2]=s[i];
    58             s[2*i+1]='#';
    59         }
    60         
    61         s[0]='!';
    62     //    cout<<s<<endl;
    63 
    64         if(manacher(s,len)==true)
    65         cout<<"Yes"<<endl;
    66         else cout<<"No"<<endl;    
    67     }
    68 
    69 } 
    
    
  • 相关阅读:
    机器学习入门-文本数据-使用聚类增加文本的标签属性
    机器学习入门-文本特征-使用LDA主题模型构造标签 1.LatentDirichletAllocation(LDA用于构建主题模型) 2.LDA.components(输出各个词向量的权重值)
    机器学习入门-文本特征-word2vec词向量模型 1.word2vec(进行word2vec映射编码)2.model.wv['sky']输出这个词的向量映射 3.model.wv.index2vec(输出经过映射的词名称)
    机器学习入门-数值特征-对数据进行log变化
    python中datetime.strptime(),strftime()的应用
    css中的','、'>'、'+'、'~'
    js中const,var,let区别
    css的#和.的区别
    js实现拖放
    [Usaco2005]Part Acquisition
  • 原文地址:https://www.cnblogs.com/noip/p/9570168.html
Copyright © 2011-2022 走看看