机试指南part3——字符串

string类型

1.构造

string s0="abc def";
    string s1;
    string s2(s0);
    string s3(s0,2,3);
    string s4("zzzzzzz");
    string s5("zzzzzzz",3);
    string s6(10,'c');
    cout<<s0<<endl;
    cout<<s1<<endl;
    cout<<s2<<endl;
    cout<<s3<<endl;
    cout<<s4<<endl;
    cout<<s5<<endl;
    cout<<s6<<endl;

2.操作

string str="hello world";
    int l=str.size();
    string str1;
    str1.insert(0,str);
    str1.insert(6,"zzz",0,2);
    str1.insert(1,"cc");
    str1.erase(6);
    str1.erase(0,2);
    cout<<str1<<endl;
    str1.clear();
    cout<<str1<<endl;

3.运算

string str1="hello world";
    string str2="111";
    string str3="222";
    string str=str1+",";
    str=str+str2+"...";
    str+=str3;
    cout<<str;

4.函数

string str;
    str.size();
    str.find("world",10);
    string str1=str.substr(10,3);

字符串

1.遍历

//特殊乘法
#include<iostream>
#include<cstdio>
#include<string>

using namespace std;

int main()
{
    string str1,str2;
    while(cin>>str1>>str2)
    {
        int answer=0;
        for(int i=0;i<str1.size();++i)
        {
            for(int j=0;j<str2.size();++j)
            {
                answer+=(str1[i]-'0')*(str2[j]-'0');
            }
        }
        cout<<answer<<endl;
    }
    return 0;
}

2.加密

#include<iostream>
#include<cstdio>
#include<string>

using namespace std;

int main()
{
    string str;
    while(getline(cin,str))
    {
        if(str=="ENDOFINPUT")
        {
            break;
        }
        getline(cin,str);
        for(int i=0;i<str.size();++i)
        {
            if('A'<=str[i]&&str[i]<='Z')
            {
                str[i]=(str[i]-'A'-5+26)%26+'A';
            }
        }
        cout<<str<<endl;
        getline(cin,str);
    }
    return 0;
}

3.统计

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>

using namespace std;

int number[128];

int main()
{
    string str1,str2;
    while(getline(cin,str1))
    {
        if(str1=="#")
        {
            break;
        }
        getline(cin,str2);
        memset(number,0,sizeof(number));
        for(int i=0;i<str2.size();++i)
        {
            number[str2[i]]++;
        }
        for(int i=0;i<str1.size();i++)
        {
            printf("%c %d\n",str1[i],number[str1[i]]);
        }
    }
    return 0;
}

4.字符串匹配
1)Detection是否出现
2)Location何处出现
3)Count出现几次
4)Enumeration每次出现在何处
匹配算法:
1.蛮力算法 O(nm)
2.kmp算法

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>

using namespace std;

const int MAXM=100;

int nextTable[MAXM];

void GetNextTable(string pattern)
{
    int m=pattern.size();
    int j=0;
    nextTable[j]=-1;
    int t=nextTable[j];
    while(j<m)
    {
       if(t==-1||pattern[t]==pattern[j])
       {
           ++t;
           ++j;
           nextTable[j]=t;
       } else
       {
           t=nextTable[t];
       }
    }
    return;
}

int KMP(string text,string pattern)
{
    GetNextTable(pattern);
    int n=text.size();
    int m=pattern.size();
    int i=0;
    int j=0;
    while(i<n&&j<m)
    {
        if(j==-1||text[i]==pattern[j])
        {
            ++i;
            ++j;
        }else
        {
            j=nextTable[j];
        }
    }
    if(j==m)
    {
        return i-j;
    }else
    {
        return -1;
    }
}

int main()
{
    string text,pattern;
    text="I love you";
    pattern="love";
    int position=KMP(text,pattern);
    cout<<position<<endl;
    return 0;
}

两个例题…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值