K.Strings in the Pocket

本文探讨了如何通过Manacher算法解决字符串反转问题,旨在找出将字符串s通过反转操作变为字符串t的所有可能方式。文章详细解释了当s等于t和不等于t时的解决方案,并通过反证法证明了其正确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、Problem:

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

2、tags:

       manacher 思维 反证法

3、Mean:

       给出字符串s,t 问有多少种方法反转s可以得到t

       n<=2e6

4、Solution:

       可以分为两种情况,分别是s==t,s!=t

       s!=t时,就看第一个不一样的位置l和最后一个不一样的位置r.判断反转以后是不是相等,相等再向外扩,直至不等.

              证明:假设反转[l,r+1]可以使s==t

                     由上述知,s[r+1]==t[r+1]

                     反转后s[l]==t[r+1]且s[r+1]==t[l],可推出s[l]==t[l]

                     又因为s[l]!=t[l]. 与已知矛盾.所以只能反转[l,r].

       s==t时,等价与求s中有多少个回文串.manacher套个板子.最终ans+=Len[i]/2 (1<=i<len)

5、Mistakes:

       开始以为是ans+=(Len[i]-1).Len[i]-1是长度. 改成只加字母情况的Len[i]-1时,接近答案,但是会漏掉一个总的解. 比如aaa->5 aa->2 aabb->4,实际上应该是6,3,6.相当于把'aaa','aa',['aa','bb']给漏了.

6、Gains:

       ans+=Len[i]/2

       cpy[i]为字母时,相当于奇数长度,以本身为中心的情况

       cpy[i]为'#'时,相当于偶数长度,对称无中心

       /2因为原本存的是长度,/2就是对数.

 

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N=2e6+5;
char s[N],t[N],cpy[2*N];
ll Len[2*N];
int work(int l,int r){
    for(int i=l,j=r;i<=r;i++,j--)
        if(s[i]!=t[j]) return 0;
    return 1;
}
void manacher(int len){
    ll Max=0,pos=0;
    for(ll i=1;i<len;i++){
        if(i<Max) Len[i]=min(Len[2*pos-i],Max-i);
        else Len[i]=1LL;
        while(cpy[i-Len[i]]==cpy[i+Len[i]]) Len[i]++;
        if(Max < i+Len[i]){
            pos=i;
            Max=i+Len[i];
        }
    }
}
ll init(){
    int len=strlen(s);
    cpy[0]='(';cpy[1]='#';
    int j=2;
    for(int i=0;i<len;i++){
        cpy[j++]=s[i];
        cpy[j++]='#';
    }
    cpy[j]=')';
    manacher(j);
    ll ans=0;
    for(int i=0;i<j;i++){
        ans+=Len[i]/2LL;
        cout<<Len[i]<<" "<<cpy[i]<<endl;
    }
    return ans;
}
int main(){
    int _;
    scanf("%d",&_);
    while(_--){
        scanf("%s",s);
        scanf("%s",t);
        int l1=strlen(s),l2=strlen(t),l=-1,r=-1;
        if(l1!=l2){
            printf("0\n");
            continue;
        }
        int f=1;
        for(int i=0;i<l1;i++)
            if(s[i]!=t[i]){
                f=0;
                if(l==-1) l=i;
                r=i;
            }
        if(!f){//different
            int F=work(l,r);
            if(!F) printf("0\n");
            else{
                for(int i=r+1,j=l-1;i<l1,j>=0;i++,j--){
                    if(s[i]==s[j]) F++;
                    else break;
                }
                printf("%d\n",F);
            }
        }else{
            ll ans = init();
            printf("%lld\n",ans);
        }
    }
}

 

### TensorFlow 字符串操作及其应用 在 TensorFlow 中,字符串操作是一项重要的功能,允许开发者处理文本数据并执行各种复杂的变换。以下是关于 TensorFlow 字符串操作的一些关键点: #### 创建字符串张量 可以使用 `tf.constant` 函数创建包含字符串的张量。 ```python import tensorflow as tf str_tensor = tf.constant(['hello', 'world']) print(str_tensor) ``` #### 基本字符串函数 TensorFlow 提供了一系列用于处理字符串的方法,这些方法能够帮助完成诸如拼接、分割以及编码解码等工作。 - **字符串连接** 通过 `tf.strings.join()` 方法可轻松地将多个字符串组合在一起[^2]。 ```python joined_str = tf.strings.join([str_tensor, str_tensor], separator=' ') result = joined_str.numpy() print(result) # 输出 b'hello world hello world' ``` - **字符串拆分** 利用 `tf.strings.split()` 可以按照指定字符来切割字符串。 ```python split_result = tf.strings.split(joined_str, sep=' ') for item in split_result.to_list(): print(item) ``` - **ASCII 编码/解码** 对于 ASCII 编码的操作,则有专门设计好的 API 如 `tf.io.decode_raw()`, 这个接口可以把原始字节流解析成整数形式表示的 ASCII 数值. ```python raw_bytes = tf.constant(b'\x48\x65\x6C\x6C\x6F') decoded_ascii = tf.io.decode_raw(raw_bytes, out_type=tf.uint8).numpy().tolist() print(decoded_ascii) # [72, 101, 76, 76, 111] ``` - **Unicode 处理** 当涉及到 Unicode 数据时,可以通过 `tf.strings.unicode_decode` 或者 `tf.strings.unicode_encode` 来进行相应的编解码工作. ```python input_unicode = tf.constant('你好世界') encoded_utf8 = tf.strings.unicode_encode(input_unicode.values, input_encoding='UTF-8').numpy() print(encoded_utf8) decoded_unicode = tf.strings.unicode_decode(tf.constant([b'\xe4\xb8\xad\xe6\x96\x87']), output_encoding='UTF-8')[0].numpy() print(decoded_unicode.decode()) ``` #### 高级字符串操作 除了上述基本功能外,还有更多高级别的工具可用于更复杂场景下的字符串预处理任务,比如正则表达式的匹配替换等。 ```python pattern = r'(\\w+) (\\w+)' replacement = '\\2 \\1' text_to_replace = ['first second'] replaced_text = tf.regex_replace(text_to_replace, pattern, replacement)[0].numpy().decode() print(replaced_text) # Outputs: "second first" ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值