Strings in the Pocket

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=6012

题意:问选字符串s的一子串反转一次得到t的方案数;

思路:s和t相同就是回文子串长度的一半,否则找最长的不相等位置,先看能不能等到t,不能就是ans=0,能就向两边扩展。

反思:多组数据不能memset!!!会tle的!!

#include<algorithm>
#include<set>
#include<queue>
#include<cmath>
#include<cstring>
#include<iostream>
#include<set>
#include<vector>
#include<queue>
#include<cmath>
#include<cstdio>
#include<map>
#include<stack>
#include<string>
#include<bits/stdc++.h>
using namespace std;

#define sfi(i) scanf("%d",&i)
#define pri(i) printf("%d\n",i)
#define sff(i) scanf("%lf",&i)
#define ll long long
#define ull unsigned long long
#define mem(x,y) memset(x,y,sizeof(x))
#define INF 0x3f3f3f3f
#define eps 1e-16
#define PI acos(-1)
#define lowbit(x) ((x)&(-x))
#define zero(x) (((x)>0?(x):-(x))<eps)
#define fl() printf("flag\n")
#define MOD(x) ((x%mod)+mod)%mod
#define endl '\n'
#define pb push_back
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)

const int maxn=2e6+9;
const int mod=1e9+7;

inline ll read()
{
    ll f=1,x=0;
    char ss=getchar();
    while(ss<'0'||ss>'9')
    {
        if(ss=='-')f=-1;ss=getchar();
    }
    while(ss>='0'&&ss<='9')
    {
        x=x*10+ss-'0';ss=getchar();
    }    return f*x;
}

char Ma[maxn*2];
int Mp[maxn*2];
char s[maxn],t[maxn];
ll Manacher(char s[],int len)
{
    //mem(Mp,0);
    int l=0;
    Ma[l++]='$';
    Ma[l++]='#';
    for(int i=0;i<len;i++)
    {
        Ma[l++]=s[i];
        Ma[l++]='#';
    }
    for(int i=0;i<=l+10;i++) Mp[i]=0;
    Ma[l]=0;
    int mx=0,id=0;
    for(int i=0;i<l;i++)
    {
        Mp[i]=mx>i?min(Mp[2*id-i],mx-i):1;
        while(Ma[i+Mp[i]]==Ma[i-Mp[i]]) Mp[i]++;
        if(i+Mp[i]>mx)
        {
            mx=i+Mp[i];
            id=i;
        }
    }
    ll ans=0;
    for(int i=0;i<l;i++)
    {
        ans+=(ll)Mp[i]/2;
    }
    return ans;
}


int main()
{
    FAST_IO;
    //freopen("input.txt","r",stdin);
    int T;
    cin>>T;
    while(T--)
    {
        cin>>s>>t;
        ll ans=0;
        int n=strlen(s);
        int l=0,r=n-1;
        while(l<n&&s[l]==t[l]) l++;
        while(r>=0&&s[r]==t[r]) r--;
        if(l==n)
        {
            ans=Manacher(s,n);
            cout<<ans<<endl;
        }
        else
        {
            ans=1;
            for(int i=l;i<=r;i++)
            {
                if(s[i]!=t[l+r-i])
                {
                    ans=0;
                    break;
                }
            }
            if(ans)
            {
                ans=1;
                l--;
                r++;
                while(l>=0&&r<n&&s[l]==t[r]&&s[r]==t[l]) l--,r++,ans++;
            }
            cout<<ans<<endl;

        }

    }

    return 0;
}

 

Here's a possible C++ program to solve the problem: ```c++ #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ifstream infile("SortedStrings.txt"); string prev_str, curr_str; bool sorted = true; // read the first string from the file if (getline(infile, prev_str)) { // read the rest of the strings and check if they are in ascending order while (getline(infile, curr_str)) { if (curr_str < prev_str) { sorted = false; cout << "The strings in the file are not sorted in ascending order.\n"; cout << "The first two out-of-order strings are:\n"; cout << prev_str << endl << curr_str << endl; break; } prev_str = curr_str; } } if (sorted) { cout << "The strings in the file are sorted in ascending order.\n"; } infile.close(); return 0; } ``` In this program, we first open the file "SortedStrings.txt" using an ifstream object. We then read the first string from the file using getline() function, and store it in the variable prev_str. We then start reading the rest of the strings from the file using another getline() function, and compare each string with the previous string. If we find a string that is smaller than the previous string, we set the sorted flag to false, and display the first two out-of-order strings. Otherwise, we continue reading the file until the end, and if all strings are sorted, we display a message indicating that the strings are sorted in ascending order. Note that in this program, we have used the string class to store the strings read from the file, and the < operator to compare two strings. This is possible because the string class overloads the < operator to perform lexicographic comparison of two strings.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值