String

本文展示了在C++和Java中进行字符串操作的方法,包括插入、删除、替换、反转、子串提取和查找。同时,文章还讨论了哈希映射在字符串替换中的应用以及使用字典树(Trie)进行字符串匹配的问题。这些示例主要针对在线判题系统(OJ)中的常见需求。
摘要由CSDN通过智能技术生成

字符串操作

#include <bits/stdc++.h>
using namespace std;

string s;

int main(){
	int cnt = 0;
	while( cin >> s ){
		cnt ++;
		cout << "Case " << cnt << ":\n";
		
		char tmp1,tmp2;
		int i,j;
		cin >> tmp1 >> i;
		s.insert(s.begin()+i, tmp1);
		cout << "Insert->" << s << endl;
		
		
		cin >> tmp1; // 参数类型是迭代器,所以用remove获取迭代器位置
		s.erase(remove(s.begin(), s.end(), tmp1), s.end());
		cout << "Erase->"  << s << endl;
		
		cin >> tmp1 >> tmp2;
		for (int i = 0; i < (int)s.length(); i++) {
			if (s[i] == tmp1) {
				s[i] = tmp2;
			}
		}
		cout << "Replace->"  << s << endl;
		
		cout << "Size->" << s.length() << endl;
		
		string reversedStr(s.rbegin(), s.rend());
		cout << "Reverse->" << reversedStr << endl;
		
		cin >> i >> j;
		cout << "Sub->" << s.substr(i, j - i + 1) << endl;
		
		string sub;
		cin >> sub;
		i = s.find(sub);
		cout << "Find->" << i << endl;
		cout << endl;
	}
	
}

OJ不能用gets()

字符串替换

 

 2.hashing strings

	import java.util.*;
 
public class Main {
 
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str =null;
        Map<String,String> map1 = new HashMap<String,String>();
        Map<String,String> map2 = new HashMap<String,String>();
        while(true) {
            str = sc.nextLine();
            if(str.equals("END")) {
                break;
            }
            String[] spli = str.split(" ");
            String tmp = spli[0];
            String curse = tmp.substring(1, tmp.length()-1); 
// 删除第一个和最后一个元素 只保留之间的
            String effect = new String();
            for(int i=1; i<spli.length; i++){
                if( i==spli.length-1){
                    effect += spli[i];
                }
                else
                    effect += spli[i] + " ";
            }
 
            map1.put(curse, effect);
            map2.put(effect, curse);
        }
 
        int n = sc.nextInt();
        for(int i=0; i<n; i++) {
            String s = sc.next();
            s += sc.nextLine();
            if(s.charAt(0)=='[') {
                s = s.substring(1, s.length()-1);
                if(map1.containsKey(s)) {
                    System.out.println(map1.get(s));
                }else {
                    System.out.println("silence");
                }
            }else {
                if(map2.containsKey(s)) {
                    System.out.println(map2.get(s));
                }else {
                    System.out.println("silence");
                }
            }
        }
    }
}

 trie字典树

#include <bits/stdc++.h>
using namespace std;

int main(){
	char str[15];
	map<string, int> m;
	string tmp;
	
	while(cin.getline(str,sizeof(str))){
		int len = strlen(str);
		if( len==0 ) 
			break;
		for(int i=len; i>0; i--){
			str[i] = '\0';
            // 将abandon abando aband aban aba ab a分别放进map
			m[str]++;
//			cout << m[str] << endl;
		}
	}
	
	while(cin.getline(str,sizeof(str))){
		if( m[str]>0 ){
			printf("YES\n");
		}
		else{
			printf("NO\n");
		}
	}
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值