http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4110

题意:反转某一个区间的子字符串,使得两个字符串相同

题解:

1、判断两个字符串是否完全相同;

2、完全相同则Manacher's Algorithm求回文字符串数量;

3、不完全相同则分别从头从尾至不相同得到区间[l,r],反转这个区间判断是否和另一个字符串相同;

4、3中不相同则ans==0;

5、3中相同则同时向两边扩张,条件是同时加1,并且相同

C++版本一

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
#define endl "\n"
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=2000000+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,p,l,r,u,v;
ll ans,cnt,flag,temp,sum;
char a[N],b[N];
int P[N<<1];
char str;
struct node{};
ll Manacher(string s)
{/*改造字符串*/string res="$#";for(int i=0;i<s.size();++i){res+=s[i];res+="#";}/*数组*/ll ans=0;int mi=0,right=0;   //mi为最大回文串对应的中心点,right为该回文串能达到的最右端的值for(int i=1;i<res.size();++i){P[i]=right>i ?min(P[2*mi-i],right-i):1;     //关键句,文中对这句以详细讲解while(res[i+P[i]]==res[i-P[i]])++P[i];if(right<i+P[i]){    //超过之前的最右端,则改变中心点和对应的最右端right=i+P[i];mi=i;}ans+=P[i]/2;}return ans;
}
int main()
{
#ifdef DEBUGfreopen("input.in", "r", stdin);//freopen("output.out", "w", stdout);
#endifios::sync_with_stdio(false);cin.tie(0);//cout.tie(0);//scanf("%d",&t);cin>>t;while(t--){//scanf("%s%s",a,b);cin>>a>>b;int len=strlen(a);for(l=0;l<len&&a[l]==b[l];l++);for(r=len-1;r>=0&&a[r]==b[r];r--);if(l>r){cout<<Manacher(a)<<endl;}else{flag=1;for(int i=l;i<=r;i++){if(a[i]!=b[r-i+l]){flag=0;break;}}if(flag){ans=1;while(l-1>=0&&r+1<=len-1&&a[l-1]==a[r+1])ans++,l--,r++;cout<<ans<<endl;}else{cout<<0<<endl;}}}#ifdef DEBUGprintf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif//cout << "Hello world!" << endl;return 0;
}

C++版本二

原博客

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<math.h>
using namespace std;
const int N=2e6+50;
char s1[N],s2[N],s[N*2];
int len[N*2];
int mi(int a,int b){if(a<b) return a;return b;
}
int main(){int t,i,n,k,p,ma;scanf("%d",&t);while(t--){scanf("%s%s",s1+1,s2+1);n=strlen(s1+1);k=1;for(i=1;i<=n;i++) if(s1[i]!=s2[i]) k=0;if(k){for(i=1;i<=n+1;i++){s[i*2]=s1[i];s[i*2-1]='*';}s[0]='#';p=ma=0;long long ans=0;for(i=1;i<=2*n;i++){if(ma>i) len[i]=mi(ma-i,len[2*p-i]);else len[i]=1;while(s[i+len[i]]==s[i-len[i]]) len[i]++;if(ma<len[i]+i){ma=len[i]+i;p=i;}ans+=len[i]/2;}printf("%lld\n",ans);}else{int l=1,r=n,ans=1;while(s1[l]==s2[l]) l++;while(s1[r]==s2[r]) r--;for(i=l;i<=r;i++) if(s1[i]!=s2[r-(i-l)]) ans=0;if(ans==0) printf("0\n");else{ma=1;while(l-ma>=1&&r+ma<=n&&s1[l-ma]==s1[r+ma]) ma++;printf("%d\n",ma);}}}return 0;
}

Strings in the Pocket相关推荐

  1. 【manacher】Strings in the Pocket

    Strings in the Pocket Time Limit: 1 Second Memory Limit: 65536 KB BaoBao has just found two strings ...

  2. 【2019浙江省赛 - K 】Strings in the Pocket(马拉车,思维)

    题干: BaoBao has just found two strings  and  in his left pocket, where  indicates the -th character i ...

  3. zoj4110 Strings in the Pocket(manacher)

    传送:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=6012 题意:给定两个串$S$和$T$,可以翻转$S$串中的任意一个子段, ...

  4. The 16th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple

    Problem A Vertices in the Pocket 比赛地址:http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?proble ...

  5. 字符串处理 —— 回文串相关

    [回文串] 回文串,就是一个正读与反读都一样的字符串,比如:abcdcba.zxccxz 等 常见的回文串算法有:求字符串中最长回文串.判断一个字符串是否为回文串.在字符中添加/删除一个字符后是否为回 ...

  6. nodejs安装服务器系统,window下,nodejs安装http-server,并开启HTTP服务器

    ASP.NET MVC Web API Post FromBody(Web API 如何正确 Post) 问题场景: ASP.NET MVC Web API 定义 Post 方法,HttpClient ...

  7. 2019 浙江省赛部分题解(The 16th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple)

    签到题 GLucky 7 in the Pocket Time Limit: 1 Second      Memory Limit: 65536 KB BaoBao loves number 7 bu ...

  8. 19浙江省赛(回文串处理)

    K - Strings in the Pocket Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & ...

  9. Pocket PC/Smartphone软件安装包之制作DIY

    Pocket PC/Smartphone软件安装包之制作DIY<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:of ...

最新文章

  1. 客户端检测的含义和方法
  2. Ehcache BigMemory: 摆脱GC困扰(转)
  3. C/C++ 函数指针调用函数
  4. linux snmp 限制ip_Windows/Linux服务器监控软件推荐
  5. nginx关闭favicon.ico日志
  6. php服务器session共享,PHP实现多WEB服务器共享SESSION的方法(2)
  7. JavaScript算法 之 选择排序
  8. 前端Swiper滑动的时候最右一个反弹回去了
  9. 拓端tecdat|R语言对BRFSS数据探索回归数据分析
  10. C#网络编程之面向连接的套接字
  11. 推荐一本go语言入门书籍
  12. 《明朝那些事儿》读后感
  13. 管道的故事(一)管道的故事
  14. ubuntu禁用smap
  15. linux安装nginx、php、mysql搭建网站
  16. 2021年中国嵌入式系统软件业务收入及业务收入结构分析[图]
  17. 创建和使用 HTTP 中间件层
  18. Unity3D——学习分享(一) 游戏开发
  19. 打造技术管理者的高端交流圈,LeaTech全球CTO领导力峰会重磅登场!
  20. android MediaPlayer架构

热门文章

  1. 计算机应用基础 专2018秋,广东开放大学远程教育专科2018年秋计算机应用基础Word模块测试.pdf...
  2. mysql 数值类型 长度_mysql中的数据类型的长度
  3. java核心api_Java核心API之字符流使用介绍
  4. php cli mysql_php – 为什么mysql CLI可以连接,但不能连接WordPress?
  5. 算法c语言源码_C语言实现推箱子
  6. 微擎小程序怎么配置服务器域名,随便撸源码源码微擎小程序通用配置图文教程,教会你怎么配置微擎小程序!...
  7. 【小白学习PyTorch教程】十五、BERT:通过PyTorch来创建一个文本分类的Bert模型
  8. 四十七、Ansible自动化入门
  9. 商城客户细分数据(kaggle)
  10. kaggle(一)训练猫狗数据集