【STL】string

本文解析了POJ平台上的两道经典算法题:POJ3087关于筹码叠放的操作模拟问题及POJ1298涉及的Caesar密码破解。详细介绍了题目背景、解题思路及C++实现代码。

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

1. 介绍


2. 基本操作


3. 问题


3.1 POJ 3087


题目大意:有两堆筹码(poker chip)S1 S2;每一次shuffle操作是指将S1 S2依次交叉叠放,形成了S12;叠放之后的split操作是指,将S12最底部的C个chip形成新的S1,余下的C个chip形成新的S2。如此往复,求形成目标的S12所需的最少次数。


直接用模拟就可以了。需要注意的是,判断循环终止条件。如果新形成的S1与输入的S1相等,说明经过若干次shuffle之后,回到了初始状况,说明这是一个死循环,形成不了目标的S12。还可以通过判断S12是否之前出现过,若出现过,即说明循环为死循环。


源代码:

3087Accepted212K0MSC++721B2013-09-29 10:24:28

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string s1,s2,result,temp1,temp2,temp;
    int N,len,i,j,steps;
	scanf("%d",&N);
	for(i=1;i<=N;i++)
	{
		scanf("%d",&len);
		cin>>s1>>s2>>result;
		steps=1;
		temp1=s1;
		temp2=s2;
		while(1)
		{
			temp="";
			for(j=0;j<len;j++)    		    /*shuffle operation*/
			{
				temp+=temp2[j];
				temp+=temp1[j];
			}
			if(temp==result)
			{
				printf("%d %d\n",i,steps);
				break;
			}
			else
			{
				for(j=0;j<len;j++)         /*split*/
				{
					temp1[j]=temp[j];
					temp2[j]=temp[j+len];
				}
				if(temp1==s1)
				{
					printf("%d -1\n",i);
					break;
				}
				steps++;
			}
		}
	}
	return 0;
}

3.2 POJ 1298


题目大意:求解Caesar密码所对应的明文。


输入比较麻烦,对C、C++的输入函数的区别比较模糊,等有空好好研究下。用cin.ignore( )处理输入START的回车。


源代码:

1298Accepted204K0MSC++573B2013-09-29 15:07:42

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string ciphertext,line;
	int i;
    while(cin>>line)
	{
		cin.ignore();
		if(line=="START")
		{
			getline(cin,ciphertext);
			for(i=0;i<ciphertext.length();i++)
			{
				if(ciphertext[i]>='F'&&ciphertext[i]<='Z')
					printf("%c",ciphertext[i]-5);
				else if(ciphertext[i]>='A'&&ciphertext[i]<='E')
					printf("%c",ciphertext[i]+21);
				else
					printf("%c",ciphertext[i]);
			}
			printf("\n");
			cin>>line;
		}
		if(line=="ENDOFINPUT")
			break;
	}
	return 0;
}



### C++ STL String Usage and Operations In the context of C++ Standard Template Library (STL), `std::string` is a fundamental component used to handle sequences of characters. This class provides various methods that facilitate string manipulation, including construction from different types, concatenation, searching within strings, and more. #### Construction Methods A `std::string` can be constructed using several constructors: - Default constructor initializes an empty string. - Constructor taking a single argument as another string or character array copies its content into this object[^1]. ```cpp #include <iostream> #include <string> int main() { std::string str; std::string copyStr("example"); } ``` #### Basic Manipulations The following are some common manipulative functions provided by `std::string`. - **Append**: Adds one string at the end of current string. - **Insert**: Inserts additional characters inside existing ones based on specified position. - **Erase**: Removes part of the sequence starting from given index up until certain length[^2]. ```cpp str.append(" world"); // Appends " world" str.insert(7, ", hello"); // Inserts ", hello" after 'e' str.erase(8, 6); // Erases six chars beginning with comma ``` #### Searching Capabilities For locating substrings or individual characters within larger texts, these utilities come handy: - **find_first_of**, **find_last_of**: Finds first/last occurrence among set of possible matches. - **substr**: Extracts substring according to start point plus size limit. ```cpp size_t pos = str.find_first_of('l'); // Returns position where 'l' appears firstly if(pos != std::string::npos){ std::cout << "Found letter l at position:" << pos; } // Get sub-string containing only last four letters std::string endingFourLetters = str.substr(str.length()-4); ``` #### Comparison Operators Comparison between two instances follows lexicographical order rules similar to those applied when comparing arrays element-wise. ```cpp bool isEqual = ("hello" == str); // Checks equality against literal value bool lessThan = ("abcde" < str); // Compares whether abcde comes before str alphabetically ``` --related questions-- 1. How does memory allocation work internally for dynamic resizing during append operations? 2. What optimizations exist specifically targeting performance improvements while performing multiple insertions consecutively? 3. Can you provide examples demonstrating how regular expressions integrate seamlessly alongside standard library features like find algorithms? 4. Are there any differences in behavior across platforms regarding case sensitivity checks performed through comparison operators?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浅唱书令

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值