字符串模拟入门

如题(水题大集锦)

1.P1914 小书童——密码

直通

思路:

  因为是循环的,所以我们进行%26即可

上代码:

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

const int Mod = 26;
const char e[Mod] = {‘a‘,‘b‘,‘c‘,‘d‘,‘e‘,‘f‘,‘g‘,‘h‘,‘i‘,‘j‘,‘k‘,‘l‘,‘m‘,‘n‘,‘o‘,‘p‘,‘q‘,‘r‘,‘s‘,‘t‘,‘u‘,‘v‘,‘w‘,‘x‘,‘y‘,‘z‘};
int n;
string y;

int main() {
    scanf("%d",&n);
    cin>>y;
    int len=y.length();
    for(int i=0,d; i<len; i++) {
        d=(y[i]-‘a‘+n)%26;
        printf("%c",e[d]);
    }
    return 0;
}


2.P1200 [USACO1.1]你的飞碟在这儿Your Ride Is He…

(Maybe是Here。。)

直通

思路:

  将字符串转化为数字之后,边乘边模即可

坑点:

  注意ret的初始值应该设置为1,而不是0,因为用到了乘

上代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

const int Mod = 47;
char s[10],p[10];

int UFO(char a[],int len) {
    int ret=1;
    for(int i=0,x; i<len; i++) {
        x=a[i]-‘A‘+1;
        ret=ret*x%Mod;
    }
    return ret;
}

int main() {
    cin>>s>>p;
    int lens=strlen(s),lenp=strlen(p);
    if(UFO(s,lens)==UFO(p,lenp)) printf("GO");
    else printf("STAY");
    return 0;
}

时间: 2024-07-30 16:55:48

字符串模拟入门的相关文章

uva--1368(贪心,字符串模拟)

点击打开链接 该题是一个带有贪心思想的字符串模拟题,题目给定m个长度为n的字符串,让你求一个长度为n的字符串,使得该字符串与这m个字符串对应位置的字符不同的个数和最小. 要使对应位置不同字符最少,则该字符串每个字符优先选择该位置出现次数多的字符,若次数相同则选择字典序更小的字符. 代码: #include <iostream> #include <cstdio> #include <string.h> #include <map> #include <

hdu 4119 Isabella&#39;s Message【字符串模拟】

题目链接:http://write.blog.csdn.net/postedit 自我感觉比较麻烦 #include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> #include<string> #include<map> using namespace std; const int maxh=100+10; const int maxe=100

大数运算之字符串模拟

相信大家被特别大的两个数据做运算折磨过.当两个操作数或者运算结果超过类型的表示范围后会有意想不到的错误,这时候我们的电脑还不如我们高中用过的科学计算器,这是作为一个程序员所不能忍受的.所以我们得找到其他的方式来计算.这就是我们今天要讨论的字符串模拟大数运算. 我们的运算一般使用int类型来算的,那么首先我们先复习一下各种int类型的数据表示范围: unsigned int 0-4294967295    int   -2147483648-2147483647  unsigned long 0-

从1打印到最大的n位数字(字符串模拟数字自加)

陷阱:  用最大的n位数-1(数字太大可能产生越界) 应该采用字符串模拟数字自加! 代码如下: #include<iostream> using namespace std; int  IsMax(char *number) {  int nLength = strlen(number);  int CarryBit = 0;  bool  ret = false;  for (int i = nLength-1; i >= 0; i--)  {   int nSum = number[

UVA 706 LCD Display 液晶显示屏 (字符串模拟)

[题目链接]click here~~ [题目大意] 给定的数字序列,按照要求输出对应液晶显示屏上的数字 输入: 2 12345 3 67890 0 0 输出: -- -- -- | | | | | | | | | | | | -- -- -- -- | | | | | | | | | | -- -- -- --- --- --- --- --- | | | | | | | | | | | | | | | | | | | | | | | | --- --- --- | | | | | | | |

UVA 10815-Andy&#39;s First Dictionary(字符串模拟+排序+重复删除)

Andy's First Dictionary Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description Problem B: Andy's First Dictionary Time limit: 3 seconds Andy, 8, has a dream - he wants to produce his very own dictionary. This

【002】}链表或字符串模拟加法/加一/乘法

链表模拟加法/字符串模拟二进制加法/数组模拟加一操作/打印1到最大的n位数/字符串模拟乘法 ============================================ Add Two Numbers 两个链表代表两个数字,每个结点的值都是一位数字,单链表逆序存放这两个数字, 构造出一个新的链表,代表这两个链表的和. 链表的尾插法,头结点dummy结点的运用,统一对prev指针的操作, C++ Code 1234567891011121314151617181920212223242

uva--232(字符串模拟)

点击打开链接 这是一道字符串模拟题,题意大概是给定一个m*n的网格,黑格用'*'表示,白格有一个字母,如果一个白格左边或者上面没有黑格子,则称为一个起始格. 然后找出所有横向单词和竖向单词,注意这个地方的横向单词指从一个起始格开始一直往右或者往下,直到遇见黑格子或者出界,并且每个字母在找横向或者竖向单词时只能用一 次 ,所以分为两个步骤,横向找和竖向找,每次判断该格子是不是起始格,并且是否在前面的单词中用过,若没用过就找下去,用过了则继续往下找起始格. #include <iostream>

zoj3826 Hierarchical Notation (字符串模拟)

Hierarchical Notation Time Limit: 2 Seconds      Memory Limit: 131072 KB In Marjar University, students in College of Computer Science will learn EON (Edward Object Notation), which is a hierarchical data format that uses human-readable text to trans