字符串专题

A.首字母大写

#include 
using namespace std;

int main() {
	string s;
	getline(cin, s);
	for(int i = 0; i < s.size(); i ++) {
		if(i == 0 && isalpha(s[i])) s[i] = toupper(s[i]);
		if(!isalpha(s[i - 1]) && isalpha(s[i])) s[i] = toupper(s[i]);
	}
	cout << s << endl;
} 

B.验证子串

#include 
using namespace std;

int main() {
	string s1;
	string s2;
	cin >> s1 >> s2;
	if(s1.find(s2) != -1)
		cout << s2 << " is substring of " << s1 << endl;
	else if(s2.find(s1) != -1) 
		cout << s1 << " is substring of " << s2 << endl;
	else cout << "No substring" << endl;
	return 0;
}  

C.大小写转换

#include 
using namespace std;

int main() {
	string a;
	while(cin >> a) {
		for(int i = 0; i < a.size(); i ++) {
			if(islower(a[i])) a[i] = toupper(a[i]);
		}
		cout << a << endl;
	}
	return 0;
}  

D.Text Reverse


#include 
#include 
#include 
using namespace std;


int main() {
	int n;
	cin >> n;
	getchar();
	while(n --) {
		string s;
		getline(cin, s);
		int lst = 0, cur = 0, i;
		string tmp;
		for(i = 0; i < s.size(); i ++) {
			if(s[i] == ' ') {
				cur = i;
				tmp = s.substr(lst, cur - lst);
				lst = cur + 1;
				reverse(tmp.begin(), tmp.end());
				cout << tmp << ' ';
			}
		}
		tmp = s.substr(cur + 1, i - cur);
		reverse(tmp.begin(), tmp.end());
		cout << tmp << endl;
	}
	return 0;
}  

E.排序

#include 
#include 
typedef long long ll;
const int N = 10001;

char s[N];
ll a[N];

int main() {
	while(~scanf("%s", s)) {
		int f = 0, idx = 0;
		ll tmp = 0;
		memset(a, 0, sizeof a);
		for(int i = 0; s[i]; i ++) {
			if(s[i] == '5') {
				if(f) a[idx ++] = tmp, f = 0;
				tmp = 0;
				continue;
			}
			tmp = tmp * 10 + s[i] - '0';
			f = 1;
		}
		if(s[strlen(s) - 1] != '5')
			a[idx ++] = tmp;
		for(int i = 1; i < idx; i ++) {
			for(int j = 0; j < idx - i; j ++) {
				if(a[j] > a[j + 1]) {
					int t = a[j];
					a[j] = a[j + 1];
					a[j + 1] = t;
				}
			}
		}
		printf("%lld", a[0]);
		for(int i = 1; i < idx; i ++) printf(" %lld", a[i]);
		puts("");
	}
	return 0;
}

F.What Are You Talking About

#include 
#include 
#include 
#include 
#include 
using namespace std;

mapmymap;
vector vs;

int main() {
	string s, s2;
	while(cin >> s && s != "END") {
		if(s == "START") continue;
		cin >> s2;
		mymap[s2] = s;
	}
	getchar();
	while(getline(cin, s) && s != "END") {
		if(s == "START") continue;
		s2 = "";
//		cout << s << endl;
		for(int i = 0; i < s.size(); i ++) {
			if(isalpha(s[i])) s2 += s[i];
			else {
				if(mymap[s2] != "") cout << mymap[s2];
				else cout << s2;
				s2 = "";
				cout << s[i];
			}
			
		}
		if(mymap[s2] != "") cout << mymap[s2];
		else cout << s2;
		cout << endl;
	}
}

G.AC Me

#include 
#include 
#include 
using namespace std;


int main() {
	string s;
	while(getline(cin, s)) {
		int a[26] = {0};
		for(int i = 0; i < s.size(); i ++) a[s[i] - 'a'] ++;
		for(int i = 0; i < 26; i ++) {
			cout << (char)(i + 97) << ':' << a[i] << endl;
		}
		cout << endl;
	}
	
	return 0;
}  

H.剪花布条

#include 
#include 
#include 
using namespace std;


int main() {
	string s1, s2;
	while(cin >> s1, s1 != "#") {
		cin >> s2;
		int cont = 0;
		int len2 = s2.size();
		for(int i = 0; i < s1.size(); i ++) {
			if(s1.substr(i, len2) == s2) cont ++, i += len2 - 1;
		}
		cout << cont << endl;
	}
	return 0;
}  

I.子串查找

#include 
#include 
#include 
using namespace std;

const int N = 1000010;

char s[N], p[N];
int ne[N];

int main() {
	scanf("%s %s", s + 1, p + 1);
	int cont = 0;
	int m = strlen(p + 1);
	int n = strlen(s + 1);
	for(int i = 2, j = 0; i <= m; i ++) {
		while(j && p[i] != p[j + 1]) j = ne[j];
		if(p[i] == p[j + 1]) j ++;
		ne[i] = j;
	}
	for(int i = 1, j = 0; i <= n; i ++) {
		while(j && s[i] != p[j + 1]) j = ne[j];
		if(s[i] == p[j + 1]) j ++;
		if(j == m) j = ne[j], cont ++;
	}
	cout << cont << endl;
	return 0;
}

J.字符串最大跨距

#include 
using namespace std;
char s[500], s1[20], s2[20];

int main() {
	string str;
	cin >> str;
	string s, s1, s2;
	int pos1, pos2;
	for(int i = 0; i < str.size(); i ++)
		if(str[i] == ',') {
			pos1 = i;
			break;
		}
	for(int i = str.size() - 1; i >= 0; i --)
		if(str[i] == ',') {
			pos2 = i;
			break;
		}
	s = str.substr(0, pos1);
	s1 = str.substr(pos1 + 1, pos2 - pos1 - 1);
	s2 = str.substr(pos2 + 1, str.size() - pos2);
	int p1, p2;
	int f1 = 0, f2 = 0;
	p1 = s.find(s1) + s1.size();
	f1 = s.find(s1);
	reverse(s.begin(), s.end());
	reverse(s2.begin(), s2.end());
	p2 = s.find(s2) + s2.size();
	f2 = s.find(s2);
	p2 = s.size() - p2;
	if(p2 - p1 < 0 || f1 == -1 || f2 == -1) cout << -1 << endl;
	else cout << p2 - p1 << endl;
	return 0;
}

K.Cyclic Nacklace

#include 
#include 
using namespace std;

const int N = 100010;

int ne[N];
char s[N];

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	int T = 1;
	cin >> T;
	while(T --) {
//		memset(ne, 0, sizeof ne);
//		memset(s, 0, sizeof s);
		cin >> (s + 1);
		int m = strlen(s + 1);
		for(int i = 2, j = 0; i <= m; i ++) {
			while(j && s[i] != s[j + 1]) j = ne[j];
			if(s[i] == s[j + 1]) j ++;
			ne[i] = j;
		}
		int l = m - ne[m];
//		cout << l << endl;
		if(l == m) cout << m << endl;
		else {
			if(m % l == 0) cout << 0 << endl;
			else cout << l - m % l << endl;
		}
	}
	return 0;
}

L.最长回文

#include 
#include 
#include 
using namespace std;

string Manacher(string s)
{
	/*改造字符串*/
	string res="$#";
	for(int i=0;i P(res.size(),0);
	int mi=0,right=0;   //mi为最大回文串对应的中心点,right为该回文串能达到的最右端的值
	int maxLen=0,maxPoint=0;    //maxLen为最大回文串的长度,maxPoint为记录中心点
	
	for(int i=1;ii ?min(P[2*mi-i],right-i):1;     //关键句,文中对这句以详细讲解
		
		while(res[i+P[i]]==res[i-P[i]])
			++P[i];
		
		if(right> s) {
		string s2 = Manacher(s);
		cout << s2.size() << endl;
	}
	return 0;
}

你可能感兴趣的:(#STL,c++,算法,开发语言)