py求助,只过了最后一个点

P5734 【深基6.例6】文字处理软件

MC_xjhjdA @ 2024-08-16 09:57:31

q=int(input())
s=input()
l=[]
o=[]
for x in range(q):
    k=input().split()
    match k[0]:
        case '1':
            s=s+k[1]
        case '2':
            s=s[int(k[1]):int(k[1])+int(k[2])+1]
        case '3':
            s=s[:int(k[1])]+k[2]+s[int(k[1]):]
        case '4':
            o.append(s.find(k[1]))
            continue
    o.append(s)
for x in o:
    print(x)

by zhizhenhuyuzhe @ 2024-08-16 10:02:24

#include<bits/stdc++.h>
using namespace std;
int n,a;
string str,c1,b1;
int b,c,d=-1,e;
int main()
{
cin>>n;
cin>>str;
for(int i=0;i<n;i++)
{
cin>>a;
if(a==1)
{
cin>>b1;
str+=b1;
cout<<str<<endl;
}
else if(a==2)
{
cin>>b>>c;
c1=str.substr(b,c);
str=c1;
cout<<str;
cout<<endl;
}
else if(a==3)
{
cin>>b>>b1;
str.insert(b,b1);
cout<<str<<endl;
}
else if(a==4)
{
cin>>b1;
if(str.find(b1)<str.size())
cout<<str.find(b1)<<endl;
else
cout<<-1<<endl;
}
}
    return 0;
}

by NC20061226 @ 2024-08-16 10:09:43

@MC_xjhjdA

def match(s,p):
    lens,lenp = len(s),len(p)
    i,j = 0,0
    while i<lens and j<lenp:
        if s[i]==p[j]:
            i+=1
            j+=1
        else:
            i-=j-1
            j = 0
    if i!=0 and j!=0:
        return i-j
    else:
        return -1
n = int(input())
l1 = input().strip('\r\n')
for i in range(n):
    str1 = input().split()
    op = int(str1[0])
    if op==1:
        l1+=str1[1]
    elif op==2:
        l1 = l1[int(str1[1]):int(str1[2])+int(str1[1])]
    elif op==3:
        l1 = l1[0:int(str1[1])]+str1[2]+l1[int(str1[1]):len(l1)]
    else:
        print(match(l1,str1[1]))
        continue
    print(l1)

by NC20061226 @ 2024-08-16 10:11:43

主要是你的 s=input() 要变成 s=input().strip('\r\n')

第一次听说 py 可以用case


by MC_xjhjdA @ 2024-08-16 12:01:50

@NC20061226 新版确实可以用


by MC_xjhjdA @ 2024-08-16 12:03:34

@NC20061226 改了之后只是多AC了第一个点


by NC20061226 @ 2024-08-16 12:03:44

@MC_xjhjdA 所以你 AC 了吗


by MC_xjhjdA @ 2024-08-16 12:06:07

@NC20061226 没有 改了s=input()那行就现在AC了#1和#5


by NC20061226 @ 2024-08-16 12:10:36

@MC_xjhjdA case 2 错了

s=s[int(k[1]):int(k[1])+int(k[2])]

by MC_xjhjdA @ 2024-08-16 12:12:56

@NC20061226 谢佬,全AC了


|