例题4.1 特殊乘法(THU)
#include<bits/stdc++.h>
using namespace std;
string s1,s2;
int main(){
while(cin>>s1>>s2){
long long ans=0;
for(int i=0;i<s1.size();i++){
for(int j=0;j<s2.size();j++){
ans+=(s1[i]-'0')*(s2[j]-'0');
}
}
cout<<ans<<endl;
}
}
例题4.2 密码翻译(PKU)
#include<bits/stdc++.h>
using namespace std;
string str;
int main(){
while(getline(cin,str)){
for(int i=0;i<str.size();i++){
if(str[i]=='z'||str[i]=='Z'){
str[i]-=25;
}
else if(str[i]>='a'&&str[i]<='z'||str[i]>='A'&&str[i]<='Z'){
str[i]++;
}
}
cout<<str<<endl;
}
}
习题4.1 skew数(PKU)
#include<bits/stdc++.h>
using namespace std;
string s;int ans;
int main(){
while(cin>>s){
ans=0;
for(int i=0;i<s.size();i++){
ans+=(s[s.size()-1-i]-'0')*(pow(2,i+1)-1);
}
cout<<ans<<endl;
}
}
习题4.2 单词替换(PKU)
用str替换指定字符串从起始位置pos开始长度为len的字符
string& replace (size_t pos, size_t len, const string& str);
#include<bits/stdc++.h>
using namespace std;
string s,a,b;
int main(){
getline(cin,s);
cin>>a>>b;
s=" "+s+" ";
a=" "+a+" ";
b=" "+b+" ";
int pos=s.find(a);
while(pos!=string::npos){
s.replace(pos,a.size(),b);
pos=s.find(a);
}
s.erase(s.begin());
cout<<s<<endl;
}
习题4.3 首字母大写(PKU)
#include<bits/stdc++.h>
using namespace std;
string s;
int main(){
while(getline(cin,s)){
for(int i=0;i<s.size();i++){
if(i==0){
s[i]=toupper(s[i]);
}
else if(s[i-1]==' '||s[i-1]=='\t'||s[i-1]=='\r'||s[i-1]=='\n'){
s[i]=toupper(s[i]);
}
}
cout<<s<<endl;
}
}
习题4.4 浮点数加法(PKU)
#include<bits/stdc++.h>
using namespace std;
string ans,sc,sd;
void add(string sa,string sb){
int ia=sa.find(".");
int ib=sb.find(".");
if(ia>ib){
sc=sa;sd=sb;
}
else{
sc=sb;sd=sa;
}
int n=abs(ia-ib);
while(n--){
sd="0"+sd;
}
int lenc=sc.size();
int lend=sd.size();
if(lenc>lend){
sa=sc;sb=sd;
}
else{
sa=sd;sb=sc;
}
n=abs(lenc-lend);
while(n--){
sb+="0";
}
int carry=0;
for(int i=sa.size()-1;i>=0;i--){
if(sa[i]=='.'){
ans="."+ans;
continue;
}
char value=sa[i]-'0'+sb[i]-'0'+carry;
ans=char(value%10+'0')+ans;
carry=value/10;
}
while(carry!=0){
ans=char(carry%10+'0')+ans;
carry/=10;
}
}
int main(){
string s1,s2;
cin>>s1>>s2;
add(s1,s2);
cout<<ans<<endl;
}
习题4.5 后缀子串排序(SJTU)
#include<bits/stdc++.h>
using namespace std;
string s;
vector<string> v;
int main(){
cin>>s;
while(s.size()){
v.push_back(s);
s.erase(s.begin());
}
sort(v.begin(),v.end());
for(int i=0;i<v.size();i++){
cout<<v[i]<<endl;
}
}
例题4.6 Number Sequence(HDU 1711)
#include<bits/stdc++.h>
using namespace std;
const int M=10010,N=1000010;
int nextTable[M],pattern[M],text[N];
void GetNextTable(int m){
int j=0;
nextTable[j]=-1;
int i=nextTable[j];
while(j<m){
if(i==-1||pattern[j]==pattern[i]){
i++,j++;
nextTable[j]=i;
}
else{
i=nextTable[i];
}
}
}
int KMP(int n,int m){
GetNextTable(m);
int i=0,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+1;
}
else{
return -1;
}
}
int main(){
int caseNumber;cin>>caseNumber;
while(caseNumber--){
int n,m;
cin>>n>>m;
for(int i=0;i<n;i++){
scanf("%d",&text[i]);
}
for(int i=0;i<m;i++){
scanf("%d",&pattern[i]);
}
cout<<KMP(n,m)<<endl;
}
}
例题4.7 Oulipo(POJ 3461)
SDU程序设计思维与实践题解
习题4.6 字符串匹配(BUAA)
#include<bits/stdc++.h>
using namespace std;
string st[1001];
int n;
int main(){
cin>>n;cin.get();
for(int i=1;i<=n;i++){
getline(cin,st[i]);
}
getline(cin,st[0]);
regex r(st[0],regex::icase);
for(int i=1;i<=n;i++){
if(regex_match(st[i],r)){
cout<<i<<" "<<st[i]<<endl;
}
}
}
习题4.7 String Matching(SJTU)
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int nextTable[N];
string pattern,text;
int m,n,ans;
void GetNextTable(){
int j=0;
nextTable[j]=-1;
int i=nextTable[j];
while(j<m){
if(i==-1||pattern[j]==pattern[i]){
i++,j++;
nextTable[j]=i;
}
else{
i=nextTable[i];
}
}
}
void KMP(){
GetNextTable();
int i=0,j=0;
while(i<n){
if(j==-1||text[i]==pattern[j]){
i++,j++;
}
else{
j=nextTable[j];
}
if(j==m){
ans++;
j=nextTable[j];
}
}
}
int main(){
cin>>text>>pattern;
m=pattern.size();
n=text.size();
KMP();
cout<<ans<<endl;
}